Created: 2017.04.18
I've always used print_r, but some tutorials use var_dump.
The
var_dump
function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.
The
print_r()
displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.
and
Generally,
print_r( )
output is nicer, more concise and easier to read, aka more human-readable but cannot show data types.
With print_r()
you can also store the output into a variable.
When requesting data via an API, var_dump
is preferable.
Using var_dump
reveals the data types used, so we can easily check whether the data is in the expected format - or whether we need to convert it, using e.g. json_decode
.
We can also see useful information like how many items are in an array, and which index an array item has.