Outputting a structured view of arrays and objects

Download php eBook


print_r() - Outputting Arrays and Objects for debugging

print_r will output a human readable format of an array or object.

You may have a variable that is an array or object. Trying to output it with an echo will throw the error:
Notice: Array to string conversion. You can instead use the print_r function to dump a human readable format of this variable.

You can pass true as the second parameter to return the content as a string.

$myobject = new stdClass();
$myobject->myvalue = 'Hello World';
$myarray = [ "Hello", "World" ];
$mystring = "Hello World";
$myint = 42;

// Using print_r we can view the data the array holds.

This outputs the following:

stdClass Object
    [myvalue] => Hello World
    [0] => Hello
    [1] => World
Hello World

Further, the output from print_r can be captured as a string, rather than simply echoed. For instance, the following code will dump the formatted version of $myarray into a new variable:

$formatted_array = print_r($myarray, true);

Note that if you are viewing the output of PHP in a browser, and it is interpreted as HTML, then the line breaks will not be shown and the output will be much less legible unless you do something like

echo '<pre>' . print_r($myarray, true) . '</pre>';

Opening the source code of a page will also format your variable in the same way without the use of the <pre> tag.

Alternatively you can tell the browser that what you're outputting is plain text, and not HTML:

header('Content-Type: text/plain; charset=utf-8');

var_dump() - Output human-readable debugging information about content of the argument(s) including its type and value

The output is more detailed as compared to print_r because it also outputs the type of the variable along with its value and other information like object IDs, array sizes, string lengths, reference markers, etc.

You can use var_dump to output a more detailed version for debugging.

var_dump($myobject, $myarray, $mystring, $myint);

Output is more detailed:

object(stdClass)#12 (1) {
  string(11) "Hello World"
array(2) {
  string(5) "Hello"
  string(5) "World"
string(11) "Hello World"

Note: If you are using xDebug in your development environment, the output of var_dump is limited / truncated by default. See the official documentation for more info about the options to change this.

var_export() - Output valid PHP Code

var_export() dumps a PHP parseable representation of the item.

You can pass true as the second parameter to return the contents into a variable.


Output is valid PHP code:

array (
  0 => 'Hello',
  1 => 'World',
'Hello World'

To put the content into a variable, you can do this:

$array_export = var_export($myarray, true);
$string_export = var_export($mystring, true);
$int_export = var_export($myint, 1); // any `Truthy` value

After that, you can output it like this:

printf('$myarray = %s; %s', $array_export, PHP_EOL);
printf('$mystring = %s; %s', $string_export, PHP_EOL);
printf('$myint = %s; %s', $int_export, PHP_EOL);

This will produce the following output:

$myarray = array (
  0 => 'Hello',
  1 => 'World',
$mystring = 'Hello World';
$myint = 42;


Contributors: 19
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook