Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

print_r()

print_r() is a built-in PHP function used for displaying the contents of an array or an object in a human-readable format.

It’s particularly helpful for debugging and understanding the structure of complex data structures in the PHP code.

When using print_r(), it displays the array or object’s values and structure to the browser or the console.

print_r() has a second parameter, which controls the direct output of the debug information to a string, instead of the standard output. This is useful to control the final destination of the message: it might be sent to a logging system, or duplicated.

print_r() carries the risk to have debugging tools published to production: while it doesn’t provide any meaningful attack surface, it is a data leak. It has no eradication mechanism, like assert().

<?php

    $a = 1;

    print_r($a);
/*
Array
(
    [0] => 1
)
*/

?>

Documentation

See Also