print_r()

print_r() — Prints human-readable information about a variable.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

print_r() displays information about a variable in a way that's readable by humans.

Signature

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

Parameters

ParameterTypeWhat it takes
$valuemixedThe expression to be printed.
$returnboolIf 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 value

If given a string, int 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.

Example

A runnable snippet:

<pre> <?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print_r($a); ?> </pre>

Output:

<pre> Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) </pre>

Changelog

VersionChange
8.4.0Return type changed from string|bool to string|true.

See also

ob_start(), var_dump(), var_export()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page print_r() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.