A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.php.net/manual/en/function.print-r.php below:

PHP: print_r - Manual

print_r

(PHP 4, PHP 5, PHP 7, PHP 8)

print_r Prints human-readable information about a variable

Description

print_r(), var_dump() and var_export() will also show protected and private properties of objects. Static class members will not be shown.

Parameters
value

The expression to be printed.

return

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 objects.

When the return parameter is true, this function will return a string. Otherwise, the return value is true.

Examples

Example #1 print_r() example

<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>

Example #2 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_rprint_r($results);
?>
Notes

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 liamtoh6 at hotmail dot com

15 years ago

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.

<?php
function print_r2($val){
echo
'<pre>';
print_r($val);
echo
'</pre>';
}
?>


It also makes sense in some cases to add an if statement to only display the output in certain scenarios, such as:

if(debug==true)


if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
simivar at gmail dot com

7 years ago

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

<?php
function print_r_reverse($input) {
$lines = preg_split('#\r?\n#', trim($input));
if (
trim($lines[ 0 ]) != 'Array' && trim($lines[ 0 ] != 'stdClass Object')) {
if ($input === '') {
return
null;
}

return

$input;
} else {
$match = array();
if (
preg_match("/(\s{5,})\(/", $lines[ 1 ], $match)) {
$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_shift($lines); array_pop($lines); $input = implode("\n", $lines);
$matches = array();
preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $input, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$pos = array();
$previous_key = '';
$in_length = strlen($input);
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) {
$ret[ $key ] = print_r_reverse(substr($input, $where[ 0 ], $where[ 1 ] - $where[ 0 ]));
}

return

$is_object ? (object)$ret : $ret;
}
}
?>

Soaku

7 years ago

liamtoh6@hotmail.com posted a function that will echo print_r output with proper new lines in HTML files. He used <pre> for it to work, but that might not be always the best method to go. For example, it is not valid to place <pre> inside <p>.

Here is my way to do this:

<?phpfunction print_rbr ($var, $return = false) {
$r = nl2br(htmlspecialchars(print_r($var, true)));
if (
$return) return $r;
else echo
$r;
}
?>


This function will:

- Place <br> where newlines are,


- Escape unsecure characters with HTML entities.
motin at demomusic dot nu

17 years ago

This works around the hacky nature of print_r in return mode (using output buffering for the return mode to work is hacky...):

<?php
function obsafe_print_r($var, $return = false, $html = false, $level = 0) {
$spaces = "";
$space = $html ? "&nbsp;" : " ";
$newline = $html ? "<br />" : "\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)
lech

8 years ago

A slight amendment to Matt's awesome print_r_reverse function (Thank You, a life-saver - data recovery :-) . If the output is copied from a Windows system, the end of lines may include the return character "\r" and so the scalar (string) elements will include a trailing "\r" character that isn't suppose to be there. To resolve, replace the first line in the function with...

<?php $lines = preg_split('#\r?\n#', trim($in)); ?>


This will work for both cases (Linux and Windows).

I include the entire function below for completeness, but all credit to Matt, the original author, Thank You.

<?php
function print_r_reverse($in) {
$lines = preg_split('#\r?\n#', trim($in));
if (
trim($lines[0]) != 'Array') {
return $in;
} else {
if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
$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_shift($lines); array_pop($lines); $in = implode("\n", $lines);
preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$pos = array();
$previous_key = '';
$in_length = strlen($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) {
$ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
}
return
$ret;
}
}
?>

Matt

15 years ago

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.

<?php
function print_r_reverse($in) {
$lines = explode("\n", trim($in));
if (
trim($lines[0]) != 'Array') {
return $in;
} else {
if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
$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_shift($lines); array_pop($lines); $in = implode("\n", $lines);
preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$pos = array();
$previous_key = '';
$in_length = strlen($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) {
$ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
}
return
$ret;
}
}
?>

reinder at fake-address dot com

19 years ago

I always use this function in my code, because most of my functions return an Array or Boolean :

<?phpfunction printr ( $object , $name = '' ) {

print (

'\'' . $name . '\' : ' ) ;

if (

is_array ( $object ) ) {
print (
'<pre>' ) ;
print_r ( $object ) ;
print (
'</pre>' ) ;
} else {
var_dump ( $object ) ;
}

}

?>


( print_r gives no output on FALSE and that can be annoying! )
anon at anon dot com

17 years ago

A slight modification to the previous post to allow for arrays containing mutli line strings. haven't fully tested it with everything, but seems to work great for the stuff i've done so far.

<?phpfunction print_r_reverse(&$output)
{
$expecting = 0; $lines = explode("\n", $output);
$result = null;
$topArray = null;
$arrayStack = array();
$matches = null;
while (!empty(
$lines) && $result === null)
{
$line = array_shift($lines);
$trim = trim($line);
if (
$trim == 'Array')
{
if (
$expecting == 0)
{
$topArray = array();
$expecting = 1;
}
else
{
trigger_error("Unknown array.");
}
}
else if (
$expecting == 1 && $trim == '(')
{
$expecting = 2;
}
else if (
$expecting == 2 && preg_match('/^\[(.+?)\] \=\> (.+)$/', $trim, $matches)) {
list (
$fullMatch, $key, $element) = $matches;
if (
trim($element) == 'Array')
{
$topArray[$key] = array();
$newTopArray =& $topArray[$key];
$arrayStack[] =& $topArray;
$topArray =& $newTopArray;
$expecting = 1;
}
else
{
$topArray[$key] = $element;
}
}
else if (
$expecting == 2 && $trim == ')') {
if (empty(
$arrayStack))
{
$result = $topArray;
}
else
{
$keys = array_keys($arrayStack);
$lastKey = array_pop($keys);
$temp =& $arrayStack[$lastKey];
unset(
$arrayStack[$lastKey]);
$topArray =& $temp;
}
}
else if (!empty($trim) && $expecting == 2)
{
$topArray[$key] .= "\n".$line;
}
else if (!empty(
$trim))
{
$result = $line;
}
}
$output = implode(n, $lines);
return
$result;
}
function print_r_reverse_multiple($output)
{
$result = array();
while ((
$reverse = print_r_reverse($output)) !== NULL)
{
$result[] = $reverse;
}
return
$result;
}
$output = '
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
[3] => Array
(
[nest] => yes
[nest2] => Array
(
[nest] => some more
asffjaskkd
)
[nest3] => o rly?
)
)
)

some extra stuff


'
;
var_dump(print_r_reverse($output), $output);?>


This should output

array(3) {


["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
array(4) {
[0]=>
string(1) "x"
[1]=>
string(1) "y"
[2]=>
string(1) "z"
[3]=>
array(3) {
["nest"]=>
string(3) "yes"
["nest2"]=>
array(1) {
["nest"]=>
string(40) "some more
asffjaskkd"
}
["nest3"]=>
string(6) "o rly?"
}
}
}
string(18) "nsome extra stuffn"

Added:


else if (!empty($trim) && $expecting == 2)
{
// Expecting close parent or element, but got just a string
$topArray[$key] .= "\n".$line;
}
preda dot vlad at yahoo dot com

12 years ago

print_r(), just like var_dump() does NOT cast an object, not even if it has a __toString() method - which is normal.

<?php
class A {
public function
__toString() {
return
'In class A';
}
}
$a = new A;
echo
$a; print_r($a); print_r((string)$a);

thbley at gmail dot com

19 years ago

Here is a print_r that produces xml:
(now you can expand/collapse the nodes in your browser)

<?php
header
('Content-Type: text/xml; charset=UTF-8');
echo
print_r_xml($some_var);

function

print_r_xml($arr,$first=true) {
$output = "";
if (
$first) $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<data>\n";
foreach(
$arr as $key => $val) {
if (
is_numeric($key)) $key = "arr_".$key; switch (gettype($val)) {
case
"array":
$output .= "<".htmlspecialchars($key)." type='array' size='".count($val)."'>".
print_r_xml($val,false)."</".htmlspecialchars($key).">\n"; break;
case
"boolean":
$output .= "<".htmlspecialchars($key)." type='bool'>".($val?"true":"false").
"</".htmlspecialchars($key).">\n"; break;
case
"integer":
$output .= "<".htmlspecialchars($key)." type='integer'>".
htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
case
"double":
$output .= "<".htmlspecialchars($key)." type='double'>".
htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
case
"string":
$output .= "<".htmlspecialchars($key)." type='string' size='".strlen($val)."'>".
htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
default:
$output .= "<".htmlspecialchars($key)." type='unknown'>".gettype($val).
"</".htmlspecialchars($key).">\n"; break;
}
}
if (
$first) $output .= "</data>\n";
return
$output;
}
?>

warhog at warhog dot net

19 years ago

For very long arrays I have written a little function which formats an array quite nice and uses javascript for browsing it like a tree. The function is very customizable with the $style parameter.
For me it's of great use for browsing large array's, for example when those are used in language-files in some script and so on. It may even be used in "real" scripts for the "real" front-end, cause the tree can very easily be styled (look at the function or the outputted source and you'll see what i mean).

Here's the function:

<?phpfunction print_r_html($arr, $style = "display: none; margin-left: 10px;")
{ static
$i = 0; $i++;
echo
"\n<div id=\"array_tree_$i\" class=\"array_tree\">\n";
foreach(
$arr as $key => $val)
{ switch (
gettype($val))
{ case
"array":
echo
"<a onclick=\"document.getElementById('";
echo
array_tree_element_$i."').style.display = ";
echo
"document.getElementById('array_tree_element_$i";
echo
"').style.display == 'block' ?";
echo
"'none' : 'block';\"\n";
echo
"name=\"array_tree_link_$i\" href=\"#array_tree_link_$i\">".htmlspecialchars($key)."</a><br />\n";
echo
"<div class=\"array_tree_element_\" id=\"array_tree_element_$i\" style=\"$style\">";
echo
print_r_html($val);
echo
"</div>";
break;
case
"integer":
echo
"<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."</i><br />";
break;
case
"double":
echo
"<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."</i><br />";
break;
case
"boolean":
echo
"<b>".htmlspecialchars($key)."</b> => ";
if (
$val)
{ echo
"true"; }
else
{ echo
"false"; }
echo
"<br />\n";
break;
case
"string":
echo
"<b>".htmlspecialchars($key)."</b> => <code>".htmlspecialchars($val)."</code><br />";
break;
default:
echo
"<b>".htmlspecialchars($key)."</b> => ".gettype($val)."<br />";
break; }
echo
"\n"; }
echo
"</div>\n"; }?>


The function as it is now does not support the $return parameter as print_r does and will create an endless loop like print_r did in php-versions < 4.0.3 when there is an element which contains a reference to a variable inside of the array to print out :-/

I've tested it with PHP 5.0.6 and PHP 4.2.3 - no problems except those already mentioned.

please e-mail me if you've got a solution for the problems i've mentioned, i myself are not able to solve them 'cause i don't know how the hell i can find out whether a variable is a reference or not.

henzeberkheij at gmail dot com

13 years ago

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!

<?php
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;
}
}
?>

Bob

16 years ago

Here is a function that formats the output of print_r as a expandable/collapsable tree list using HTML and JavaScript.
<?php
function print_r_tree($data)
{
$out = print_r($data, true);$out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">\\2</a><div id=\"'.\$id.'\" style=\"display: none;\">'", $out);$out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
}
?>
Pass it a multidimensional array or object and each sub-array/object will be hidden and replaced by a html link that will toggle its display on and off.
Its quick and dirty, but great for debugging the contents of large arrays and objects.
Note: You'll want to surround the output with <pre></pre>
machuidel

14 years ago

The following will output an array in a PHP parsable format:

<?php
function serialize_array(&$array, $root = '$root', $depth = 0)
{
$items = array();

foreach(

$array as $key => &$value)
{
if(
is_array($value))
{
serialize_array($value, $root . '[\'' . $key . '\']', $depth + 1);
}
else
{
$items[$key] = $value;
}
}

if(

count($items) > 0)
{
echo
$root . ' = array(';$prefix = '';
foreach(
$items as $key => &$value)
{
echo
$prefix . '\'' . $key . '\' => \'' . addslashes($value) . '\'';
$prefix = ', ';
}

echo

');' . "\n";
}
}
?>

Anonymous

16 years ago

I have written a nice debugging function.
This function handles arrays beautifully.
<?php
function DebugDisplayVar($input, $name = "Debug", $i = "0", $k = array("Error")){
if(
is_array($input))
{ foreach (
$input as $i => $value){
$temp = $k;
$temp[] = $i;
DebugDisplayVar($value, $name, $i, $temp);}
}else{
echo "$".$name;foreach ($k as $i => $value){
if(
$value !=="Error"){echo "[$value]";}
}
echo
" = $input<br>";
} }
Debug[0] = value
Debug
[1] = another value
ect
...?>
Matthew Ruivo (mruivo at gmail)

18 years ago

For those of you needing to print an array within a buffer callback function, I've created this quick function. It simply returns the array as a readable string rather than printing it. You can even choose whether to return it in normal text-mode or HTML. It's recursive, so multi-dimensial arrays are supported. I hope someone finds this useful!

<?phpfunction return_array($array, $html = false, $level = 0) {
$space = $html ? "&nbsp;" : " ";
$newline = $html ? "<br />" : "\n";
for (
$i = 1; $i <= 6; $i++) {
$spaces .= $space;
}
$tabs = $spaces;
for (
$i = 1; $i <= $level; $i++) {
$tabs .= $spaces;
}
$output = "Array" . $newline . $newline;
foreach(
$array as $key => $value) {
if (
is_array($value)) {
$level++;
$value = return_array($value, $html, $level);
$level--;
}
$output .= $tabs . "[" . $key . "] => " . $value . $newline;
}
return
$output;
}
?>


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3