print_r() displays information about a variable in a way that's readable by humans. If given a string, integer 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.
Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning.
Tip: As with anything that outputs its result directly to the browser, you can use the output-control functions to capture the output of this function, and save it - for example - in a string.
<pre> <?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z')); print_r ($a); ?> </pre> |
Which will output:
<pre> Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) </pre> |
Note: Prior to PHP 4.0.4, print_r() will continue forever if given an array or object that contains a direct or indirect reference to itself. An example is print_r($GLOBALS) because $GLOBALS is itself a global variable that contains a reference to itself.
See also ob_start(), var_dump(), and var_export().