print_r
print_r() displays information about a variable in a way that’s readable by humans.
print_r() , var_dump() and var_export() will also show protected and private properties of objects. Static class members will not be shown.
Parameters
The expression to be printed.
If you would like to capture the output of print_r() , use the return parameter. When this parameter is set to true , print_r() will return the information rather than print it.
Return Values
If given a string , int 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 object s.
When the return parameter is true , this function will return a string . Otherwise, the return value is true .
Examples
Example #1 print_r() example
$a = array ( 'a' => 'apple' , 'b' => 'banana' , 'c' => array ( 'x' , 'y' , 'z' ));
print_r ( $a );
?>
The above example will output:
Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) )Example #2 return parameter example
$b = array ( ‘m’ => ‘monkey’ , ‘foo’ => ‘bar’ , ‘x’ => array ( ‘x’ , ‘y’ , ‘z’ ));
$results = print_r ( $b , true ); // $results now contains output from print_r
?>?phpNotes
Note:
When the return parameter is used, this function uses internal output buffering prior to PHP 7.1.0, so it cannot be used inside an ob_start() callback function.
See Also
- ob_start() — Turn on output buffering
- var_dump() — Dumps information about a variable
- var_export() — Outputs or returns a parsable string representation of a variable
User Contributed Notes 36 notes
I add this function to the global scope on just about every project I do, it makes reading the output of print_r() in a browser infinitely easier.
function print_r2 ( $val ) echo ‘
' ;
print_r ( $val );
echo '
‘ ;
>
?>
It also makes sense in some cases to add an if statement to only display the output in certain scenarios, such as:
I’ve fixed function wrote by Matt to reverse print_r — it had problems with null values. Created a GIST for that too so please add any future fixes in there instead of this comment section:
https://gist.github.com/simivar/037b13a9bbd53ae5a092d8f6d9828bc3
return $input ;
> else // this is an array or object, lets parse it
$match = array();
if ( preg_match ( «/(\s)\(/» , $lines [ 1 ], $match )) // this is a tested array/recursive call to this function
// take a set of spaces off the beginning
$spaces = $match [ 1 ];
$spaces_length = strlen ( $spaces );
$lines_total = count ( $lines );
for ( $i = 0 ; $i < $lines_total ; $i ++) if ( substr ( $lines [ $i ], 0 , $spaces_length ) == $spaces ) $lines [ $i ] = substr ( $lines [ $i ], $spaces_length );
>
>
>
$is_object = trim ( $lines [ 0 ]) == ‘stdClass Object’ ;
array_shift ( $lines ); // Array
array_shift ( $lines ); // (
array_pop ( $lines ); // )
$input = implode ( «\n» , $lines );
$matches = array();
// make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
preg_match_all ( «/^\s\[(.+?)\] \=\> /m» , $input , $matches , PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
$pos = array();
$previous_key = » ;
$in_length = strlen ( $input );
// store the following in $pos:
// array with key = key of the parsed array’s item
// value = array(start position in $in, $end position in $in)
foreach ( $matches as $match ) $key = $match [ 1 ][ 0 ];
$start = $match [ 0 ][ 1 ] + strlen ( $match [ 0 ][ 0 ]);
$pos [ $key ] = array( $start , $in_length );
if ( $previous_key != » ) $pos [ $previous_key ][ 1 ] = $match [ 0 ][ 1 ] — 1 ;
>
$previous_key = $key ;
>
$ret = array();
foreach ( $pos as $key => $where ) // recursively see if the parsed out value is an array too
$ret [ $key ] = print_r_reverse ( substr ( $input , $where [ 0 ], $where [ 1 ] — $where [ 0 ]));
>
return $is_object ? (object) $ret : $ret ;
>
>
?>
Here is another version that parses the print_r() output. I tried the one posted, but I had difficulties with it. I believe it has a problem with nested arrays. This handles nested arrays without issue as far as I can tell.
function print_r_reverse ( $in ) <
$lines = explode ( «\n» , trim ( $in ));
if ( trim ( $lines [ 0 ]) != ‘Array’ ) <
// bottomed out to something that isn’t an array
return $in ;
> else <
// this is an array, lets parse it
if ( preg_match ( «/(\s)\(/» , $lines [ 1 ], $match )) <
// this is a tested array/recursive call to this function
// take a set of spaces off the beginning
$spaces = $match [ 1 ];
$spaces_length = strlen ( $spaces );
$lines_total = count ( $lines );
for ( $i = 0 ; $i < $lines_total ; $i ++) <
if ( substr ( $lines [ $i ], 0 , $spaces_length ) == $spaces ) <
$lines [ $i ] = substr ( $lines [ $i ], $spaces_length );
>
>
>
array_shift ( $lines ); // Array
array_shift ( $lines ); // (
array_pop ( $lines ); // )
$in = implode ( «\n» , $lines );
// make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
preg_match_all ( «/^\s\[(.+?)\] \=\> /m» , $in , $matches , PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
$pos = array();
$previous_key = » ;
$in_length = strlen ( $in );
// store the following in $pos:
// array with key = key of the parsed array’s item
// value = array(start position in $in, $end position in $in)
foreach ( $matches as $match ) <
$key = $match [ 1 ][ 0 ];
$start = $match [ 0 ][ 1 ] + strlen ( $match [ 0 ][ 0 ]);
$pos [ $key ] = array( $start , $in_length );
if ( $previous_key != » ) $pos [ $previous_key ][ 1 ] = $match [ 0 ][ 1 ] — 1 ;
$previous_key = $key ;
>
$ret = array();
foreach ( $pos as $key => $where ) <
// recursively see if the parsed out value is an array too
$ret [ $key ] = print_r_reverse ( substr ( $in , $where [ 0 ], $where [ 1 ] — $where [ 0 ]));
>
return $ret ;
>
>
This works around the hacky nature of print_r in return mode (using output buffering for the return mode to work is hacky. ):
/**
* An alternative to print_r that unlike the original does not use output buffering with
* the return parameter set to true. Thus, Fatal errors that would be the result of print_r
* in return-mode within ob handlers can be avoided.
*
* Comes with an extra parameter to be able to generate html code. If you need a
* human readable DHTML-based print_r alternative, see http://krumo.sourceforge.net/
*
* Support for printing of objects as well as the $return parameter functionality
* added by Fredrik Wollsén (fredrik dot motin at gmail), to make it work as a drop-in
* replacement for print_r (Except for that this function does not output
* paranthesises around element groups. 😉 )
*
* Based on return_array() By Matthew Ruivo (mruivo at gmail)
* (http://se2.php.net/manual/en/function.print-r.php#73436)
*/
function obsafe_print_r ( $var , $return = false , $html = false , $level = 0 ) $spaces = «» ;
$space = $html ? » » : » » ;
$newline = $html ? «
» : «\n» ;
for ( $i = 1 ; $i <= 6 ; $i ++) $spaces .= $space ;
>
$tabs = $spaces ;
for ( $i = 1 ; $i <= $level ; $i ++) $tabs .= $spaces ;
>
if ( is_array ( $var )) $title = «Array» ;
> elseif ( is_object ( $var )) $title = get_class ( $var ). » Object» ;
>
$output = $title . $newline . $newline ;
foreach( $var as $key => $value ) if ( is_array ( $value ) || is_object ( $value )) $level ++;
$value = obsafe_print_r ( $value , true , $html , $level );
$level —;
>
$output .= $tabs . «[» . $key . «] => » . $value . $newline ;
>
if ( $return ) return $output ;
else echo $output ;
>
?>
Built on a function earlier posted in these comments as stated in the Doc comment. Cheers! /Fredrik (Motin)
print_r is used for debug purposes. Yet I had some classes where I just wanted the values coming out of the database, not all the other crap. thus i wrote the following function. If your class has an toArray function, that one will be called otherwise it will return the object as is. print_neat_classes_r is the function that should be called!
print_neat_classes_r ( $array , $return = false ) <
return print_r ( self :: neat_class_print_r ( $array ), $return );
>
function do_print_r ( $array , $return = false ) <
if( is_object ( $array ) && method_exists ( $array , ‘toArray’ )) <
return $array -> toArray ();
>else if( is_array ( $array )) <
foreach( $array as $key => $obj ) <
$array [ $key ] = self :: do_print_r ( $obj , $return );
>
return $array ;
>else <
return $array ;
>
>
?>
I always use this function in my code, because most of my functions return an Array or Boolean :
function printr ( $object , $name = » )
if ( is_array ( $object ) ) print ( ‘
' ) ;
print_r ( $object ) ;
print ( '
‘ ) ;
> else var_dump ( $object ) ;
>
?>
( print_r gives no output on FALSE and that can be annoying! )
print_r(), just like var_dump() does NOT cast an object, not even if it has a __toString() method — which is normal.
class A public function __toString () return ‘In class A’ ;
>
>
$a = new A ;
echo $a ; // In class A
print_r ( $a ); // A Object()
// you can simulate the echo by casting it manually
print_r ((string) $a ); // In class A
Here is a function that formats the output of print_r as a expandable/collapsable tree list using HTML and JavaScript.
function print_r_tree ( $data )
// capture the output of print_r
$out = print_r ( $data , true );
// replace ‘)’ on its own on a new line (surrounded by whitespace is ok) with ‘