print_r()
The print_r() function is a built-in function in PHP that prints a human-readable representation of a variable to the output. It is commonly used for debugging
Introduction
print_r() is a built-in PHP function that prints a human-readable representation of a variable. For scalars (strings, numbers, booleans) it simply prints the value, but its real value is with arrays and objects: it lays out the keys and values as an indented tree, so you can see the structure at a glance.
It is one of the three go-to debugging functions in PHP, alongside var_dump() and var_export(). Reach for print_r() when you want a quick, readable look at what's inside a variable.
Syntax
print_r(mixed $value, bool $return = false): string|true| Parameter | Description |
|---|---|
$value | The variable to display. Any type — scalar, array, or object. |
$return | Optional. When false (the default), the output is printed directly. When true, the output is returned as a string instead of being printed, so you can capture it in a variable. |
The return value depends on $return: with the default false it returns true; with true it returns the formatted string.
Printing an array
print_r() shows nested arrays as an indented tree, which makes it easy to read multi-level data:
Each key appears as [key] => value, and the nested roles array is indented under its parent:
Array
(
[name] => Alice
[age] => 30
[roles] => Array
(
[0] => admin
[1] => editor
)
)Capturing the output as a string
Pass true as the second argument to return the formatted text instead of printing it. This is handy for writing the structure to a log file, embedding it in an error message, or wrapping it in HTML:
<?php
$data = ["x" => 1, "y" => 2];
$text = print_r($data, true); // captured, not printed
error_log($text); // e.g. send it to the log
echo strtoupper(substr($text, 0, 5)); // prints: ARRAY
?>Tip: wrap it in <pre> for the browser
In a web page, browsers collapse the indentation and newlines that print_r() relies on, so the output looks like one run-on line. Wrap it in a <pre> tag (or capture with $return and echo inside <pre>) to keep the formatting readable:
<?php
echo "<pre>";
print_r($person);
echo "</pre>";
?>print_r() vs. var_dump() vs. var_export()
These three functions all inspect variables, but they serve different needs:
print_r()— the most readable. Best for a quick human-eye check of structure. It does not show data types or string lengths.var_dump()— shows the type and size of every value (int(30),string(5) "Alice"), and can print several variables at once. Best when the type matters (e.g. distinguishing0,"0",false, andnull).var_export()— outputs valid PHP code that recreates the variable. Best when you want a value you can paste back into source or cache.
If you only need to know what type a single variable is, gettype() is more direct.
Conclusion
print_r() is the friendliest of PHP's inspection tools: it prints arrays and objects as a clean, indented tree that's easy to scan while debugging. Use the default mode to dump a variable straight to the output, pass true to capture the result as a string, and wrap the call in <pre> when you're viewing it in a browser. When you also need types and sizes, switch to var_dump(); when you need reusable PHP code, use var_export().