print_r

Prints human-readable information about a variable

Syntax

print_r(mixed $value, bool $return = false): string|bool

Parameters

value

The value to be printed.

return

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.

Return

If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects. When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.

Examples

value | bool

<?

$value = false;

print_r($value);

?>

			
		

value | int

<?

$value = 0;

print_r($value);

?>
0

value | float

<?

$value = 0.0;

print_r($value);

?>
0

value | string

<?

$value = "0";

print_r($value);

?>
0

value | array

<?

$value = array(0, 1, array(0, 1));

print_r($value);

?>
Array
(
    [0] => 0
    [1] => 1
    [2] => Array
        (
            [0] => 0
            [1] => 1
        )

)

value | object | standard class

<?

$value = new stdclass;
$value->var = 0;

print_r($value);

?>
stdClass Object
(
    [var] => 0
)

value | object | custom class

<?

class myclass
{
    public $var;
}

$value = new myclass;
$value->var = 0;

print_r($value);

?>
myclass Object
(
    [var] => 0
)

return | false

<?

class myclass
{
    public $var;
}

$value = new myclass;
$value->var = 0;
$return = false;

$output = print_r($value, $return);

echo "output: " . PHP_EOL;
var_export($output);

?>
myclass Object
(
    [var] => 0
)
output: 
true

return | true

<?

class myclass
{
    public $var;
}

$value = new myclass;
$value->var = 0;
$return = true;

$output = print_r($value, $return);

echo "output: " . PHP_EOL . $output;

?>
output: 
myclass Object
(
    [var] => 0
)

Internal

var_dump

var_export

External

PHP

Home Menu