PHP Function Reference

PHP print_r() Function



The PHP print_r() function prints the information about a variable in a more human-readable way.

Note: print_r(), var_dump() and var_export() will also show protected and private properties of objects. Static class members will not be shown.

Syntax

print_r(variable, return)

Parameters

variable Required. Specify the variable to be printed.
return Optional. If set to true, this function will returns the information rather than print it. Default is false

Return Value

If the variable is a string, int or float, the value itself will be printed. If the variable is 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 returns a string.

Example:

The example below shows the usage of print_r() function.

<?php
$x = array(10, 20, array("a", "b"));
print_r($x);

echo "\n";

$y = array(10=>"Red", 20=>"Green", 30=>"Blue",
           40=>array("Black", "White"));
print_r($y);
?>

The output of the above code will be:

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

Array
(
    [10] => Red
    [20] => Green
    [30] => Blue
    [40] => Array
        (
            [0] => Black
            [1] => White
        )
)

❮ PHP Variable Handling Reference