390 of 410 menu

The print_r Function

The print_r function outputs information about a variable in a human-readable form. The first parameter is the variable to output, the second parameter (optional) - if set to true, returns the result as a string instead of outputting it.

Syntax

print_r(mixed $value, bool $return = false);

Example

Let's output information about an array:

<?php $arr = [1, 2, 3, 4, 5]; print_r($arr); ?>

Code execution result:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

Example

Get the result as a string instead of output:

<?php $arr = ['a', 'b', 'c']; $res = print_r($arr, true); echo $res; ?>

Code execution result:

Array ( [0] => a [1] => b [2] => c )

Example

Output information about a string:

<?php $str = 'Hello world'; print_r($str); ?>

Code execution result:

Hello world

See Also

  • the var_dump function,
    which outputs detailed information about a variable
  • the var_export function,
    which returns a string representation of a variable
byenru