print_r
Prints a human-readable representation of any value, or returns it as a string.
print_r(mixed $value, bool $return = false): string|true- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The expression to be printed. |
$returnoptional | bool= false | When true, print_r() returns the information as a string instead of printing it. |
Return value
string|trueThe formatted representation as a string when return is true; otherwise true after printing.
Changelog
- PHP 8.4.0The return type changed from string|bool to string|true. print_r() never returns false.
print_r() renders arrays and objects as an indented tree that is easy to read at a glance. Scalars print as their plain value. It shows private and protected object properties (annotated :private and :protected) but never static members.
When to reach for it
It is the fastest way to see the shape of an array during development. Pass true as the second argument to capture the output as a string instead of printing it — essential when you want to log it, wrap it in <pre> tags, or include it in an exception message. In the CLI, follow it with PHP_EOL; in a browser, wrap it: echo '<pre>', print_r($v, true), '</pre>';
What trips people up
print_r() hides types. true prints as 1, false and null print as nothing at all, and "0" is indistinguishable from 0. That empty output is the single most common source of "my value disappeared" confusion. When types matter, use var_dump(), which shows type and length for every value.
Recursive structures print *RECURSION* rather than looping forever, and the output is not valid PHP — use var_export() if you need something you can paste back into code.
Example
When to use it
- Inspecting the shape of a decoded JSON payload while debugging
- Capturing an array as a string to include in a log line or exception message
- Printing a config array in a CLI script to confirm what was loaded
Frequently asked questions
What is the difference between print_r and var_dump?
var_dump() shows the type and length of every value and handles booleans and null explicitly, while print_r() prints a compact human-readable tree without type information. Use var_dump() when types matter.
How do I get print_r output as a string?
Pass true as the second argument: $s = print_r($value, true). With the default false it prints directly and returns true.
Why does print_r show nothing for my variable?
The value is almost certainly null or false — print_r() renders both as an empty string. Confirm with var_dump(), which prints NULL and bool(false) explicitly.
More examples
Capturing output for a log line
The return argument turns print_r() into a string builder. Its multi-line format is awkward on one log line, which is why json_encode() is usually the better choice for structured logging.
<?php
function logLine(string $msg, array $context): string {
return $msg . ' ' . str_replace("\n", '', print_r($context, true));
}
echo logLine('order.failed', ['id' => 7, 'reason' => 'card_declined']), "\n";
$json = json_encode(['id' => 7, 'reason' => 'card_declined']);
echo 'json.failed ', $json, "\n";print_r versus var_dump versus var_export
print_r() shows blanks for false, '' and null, making them impossible to tell apart. var_export() produces valid PHP you can paste into a file, and var_dump() labels every type and string length.
<?php
$v = ['on' => true, 'off' => false, 'zero' => 0, 'empty' => '', 'nil' => null];
echo "--- print_r ---\n";
print_r($v);
echo "--- var_export ---\n";
var_export($v);
echo "\n--- var_dump ---\n";
var_dump($v);
Discussion