MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP Outputting a structured view of arrays and objects

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.
print_r($myobject);
print_r($myarray);
print_r($mystring);
print_r($myint);

Output

stdClass Object
(
[myvalue] => Hello World
)
Array
(
[0] => Hello
[1] => World
)
Hello World
42

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

 tag.


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

object(stdClass)#12 (1) {
["myvalue"]=>
string(11) "Hello World"
}
array(2) {
[0]=>
string(5) "Hello"
[1]=>
string(5) "World"
}
string(11) "Hello World"
int(42)

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.


var_export($myarray);
var_export($mystring);
var_export($myint);

Output

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

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

Output

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

Output

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


Conclusion

In this page (written and validated by ) you learned about PHP Outputting a structured view of arrays and objects . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Outputting large integers.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.