Signature
print_r(mixed $value, bool $return = false): string|truePurpose
print_r() shows the contents of a variable in a layout meant for people to read. It is most often used while debugging arrays and objects, because it lays out keys and values on separate lines and indents nested structures. For strings, integers and floats it simply prints the value itself.
It is a quick inspection tool, not a serialization format. If you need output that is valid PHP code, look at var_export(). If you need exact types and lengths, look at var_dump().
Signature
Parameters and defaults
$value: the expression to describe. Any type is accepted.$return: defaults tofalse. Withfalse, the text is sent straight to output. Withtrue, nothing is printed and the text is returned as a string, so you can store it, log it or pass it on.
Return type and failure cases
Since PHP 8.4.0 the declared return type is string|true; earlier versions declared string|bool. In practice the behavior is simple: when $return is true you get a string, and otherwise you get true after the text has been printed. The function has no normal failure result to check for.
Because print_r() prints text by default, remember that its return value is not the text unless you ask for it.
Example 1: printing a nested array
Each nesting level is indented further, and a nested array is followed by a blank line.
$data = ['a' => 'apple', 'b' => 'banana', 'c' => ['x', 'y', 'z']];
print_r($data);Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
The output is:
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
Example 2: capturing the output
Pass true as the second argument to get a string. Here the string is encoded as JSON so that every newline is visible.
$text = print_r(['m' => 'monkey', 'n' => 1], true);
echo json_encode($text, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";"Array\n(\n [m] => monkey\n [n] => 1\n)\n"
The output is:
"Array\n(\n [m] => monkey\n [n] => 1\n)\n"
Note that the captured text ends with a newline after the closing parenthesis.
Example 3: scalar values
Scalars are converted the same way as when you use echo. This means true becomes 1, while false and null become empty strings.
$values = [true, false, null, 3.0, 'text'];
$out = [];
foreach ($values as $v) {
$out[] = print_r($v, true);
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["1","","","3","text"]
The output is:
["1","","","3","text"]
Edge cases
- Objects: the output starts with the class name followed by
Object. Protected and private properties are included and marked, for example[y:Point:private]or[z:protected]. Static class members are not shown. - Empty arrays: these print the
Arrayheader and a pair of parentheses with nothing between them. - Lost type information:
false,nulland an empty string all look the same when a scalar is printed, and1can be the integer1ortrue. Usevar_dump()when the type matters. - Recursive structures: an object that refers back to itself is cut off with a recursion marker rather than looping forever.
- Output buffering: the manual notes that before PHP 7.1.0, using
$returnrelied on internal output buffering, so it could not be used inside anob_start()callback. That limitation does not apply to PHP 8.4.
Common mistakes
- Echoing the result of
print_r($x). Withouttrue, the function already prints and returnstrue, soecho print_r($x)adds a stray1to the output. - Forgetting that it is not code. The format cannot be pasted back into PHP or parsed reliably. Use
var_export()orjson_encode()when another program needs to read the data. - Relying on it for types. Strings, numbers and booleans can look alike, which makes bugs hard to see.
- Leaving debug output in production code. Capture the string with
trueand send it to a logger instead of printing it into a response. - Expecting browser-friendly layout. In HTML, whitespace collapses, so the indentation vanishes unless the text is shown in a preformatted block or escaped properly.
Related functions
var_dump()prints types, lengths and values for debugging.var_export()returns or prints a parsable PHP representation.ob_start()turns on output buffering, which is another way to capture printed output.json_encode()produces a portable text format for structured data.
Recap
print_r() gives a readable view of any value. By default it prints and returns true; with $return set to true it returns the text as a string. It is good for quick inspection of arrays and objects, but it hides some type distinctions, so choose var_dump() or var_export() when you need precision.
Tested examples
$data = ['a' => 'apple', 'b' => 'banana', 'c' => ['x', 'y', 'z']];
print_r($data);Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
$text = print_r(['m' => 'monkey', 'n' => 1], true);
echo json_encode($text, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";"Array\n(\n [m] => monkey\n [n] => 1\n)\n"
$values = [true, false, null, 3.0, 'text'];
$out = [];
foreach ($values as $v) {
$out[] = print_r($v, true);
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["1","","","3","text"]