search for in the  
<issetserialize>
Last updated: Thu, 19 May 2005

print_r

(PHP 4, PHP 5)

print_r --  Prints human-readable information about a variable

Description

bool print_r ( mixed expression [, bool return] )

Note: The return parameter was added in PHP 4.3.0

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. print_r() and var_export() will also show protected and private properties of objects with PHP 5, on the contrary to var_dump().

Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning.

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

The above example will output:

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

If you would like to capture the output of print_r(), use the return parameter. If this parameter is set to TRUE, print_r() will return its output, instead of printing it (which it does by default).

Example 1. return parameter example

<?php
   $b
= array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z'));
  
$results = print_r($b, true); //$results now contains output from print_r
?>

Note: If you need to capture the output of print_r() with a version of PHP prior to 4.3.0, use the output-control functions.

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().



User Contributed Notes
print_r
james at jyopp <dotcom>
04-May-2005 01:56
Here's a simpler way to use print_r to get readable output to the browser:

<?php
  
print "<pre>".print_r($var, true)."</pre>";
?>

It's clean, fast, and hasn't had any problems for me so far...
cmadole [at] jamspiritsites [dot] com
18-Mar-2005 03:06
I've found this to be handy adaptation of print_r for outputing arrays to a browser; it saves you from having to look at the source code.

<?php
function html_print_r($object, $returnValue = false) {
  
$output = print_r($object, true);
  
$output = htmlentities($output);
  
$output = str_replace(" ", "&nbsp;", );
  
$output = nl2br($output);

   if (
$returnValue) {
       return
$output;
   } else {
       echo
$output;
       return
true;
   }
}
?>
general at bugfoo dot com
20-Jan-2005 08:56
Correction to below: you can't use them in a function which is a callback of ob_start().
general at NOSPAMbugfoo dot com
20-Jan-2005 03:23
You can't use print_r($var, TRUE) if you are already buffering or inside a function which is buffering or you get the following error:
Fatal error: print_r(): Cannot use output buffering in output buffering display handlers
peranders at peranders dot net
26-Oct-2004 10:13
These functions will print human-readable information
about a variable in HTML format
Only tested in PHP Version 4.3.1
<?
$person
=new stdClass();
$person->name="Jonny";
$person->interests=array('Fishing','Hiking');

echo
"<H1>marr (PHP 4 , PHP 5) </H1>";
marr($person);
echo
"<H1>marr for all versions 4.3.x above</H1>";
marr_php_version_4_3($person);

function
marr($a)
{
  
ob_start();
  
print_r($a);
  
$t= ob_get_contents();
  
ob_end_clean();
   for(
$c=10;$c>=1;$c--)
   {
      
$search="\n ".str_repeat(" ",4*$c-1);
      
$replace="<br>\n".str_repeat("&nbsp;",8*$c);
      
$t= str_replace($search,$replace,$t);
   }
  
//Final adjustment which takes care of the single last closing parenthesis
  
$t= str_replace("\n\n)","<br>\n)",$t);

   echo
$t;
}

function
marr_php_version_4_3($a)
{
  
$t=print_r($a,true);
   for(
$c=10;$c>0;$c--)
   {
      
$search="\n ".str_repeat(" ",4*$c-1);
      
$replace="<br>\n".str_repeat("&nbsp;",8*$c);
      
$t= str_replace($search,$replace,$t);
   }
  
//Final adjustment which takes care of the single last closing parenthesis
  
$t= str_replace("\n\n)","<br>\n)",$t);
   echo
$t;
}
?>

<issetserialize>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT