To print information about variables, arrays or objects in PHP, we can use the following functions:
print_r()
var_dump()
var_export()
print_r( )
According to this explanation on stackoverflow.com, print_r()
:
- Outputs a human-readable representation of any one value
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Useful when debugging
- May return its output as a return value (instead of echoing) if the second optional argument is given
Ostensibly, the ‘r’ in print_r stands for ‘readable’. For example, setting up an associative array and using print_r to print it in a human readable format:
<?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print_r ($a); ?>
yields:
Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) )
For more information about print_r()
, see:
http://php.net/manual/en/function.print-r.php
var_dump( )
Here is what php.net says about var_dump()
:
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
According to this explanation on stackoverflow.com, var_dump()
:
- Outputs a human-readable representation of one or more values separated by commas
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Uses a different output format to print_r(), for example it also prints the type of values
- Useful when debugging
- No return value
For example, let’s set up an array that contains two integers and a sub-array. var_dump()
will not only show the contents of the array, but will also show the data types and lengths of each value in the array:
<?php $a = array(1, 2, array("apple", "banana", "cherry")); var_dump($a); ?>
yields:
array(3) { [0]=> int(1) [1]=> int(2) [2]=> array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(6) "cherry" } }
Another example of using var_dump()
to get data type of a variable:
<?php $b = 3.1; $c = true; var_dump($b, $c); ?>
which yields:
float(3.1) bool(true)
For more information about var_dump()
, see:
http://php.net/manual/en/function.var-dump.php
var_export( )
Here is what php.net says about var_export()
:
var_export — Outputs or returns a parsable string representation of a variable
var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.
According to this explanation on stackoverflow.com, var_export()
:
- Outputs a human-readable and PHP-executable representation of any one value
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Uses a different output format to both
print_r()
andvar_dump()
– resulting output is valid PHP code!- Useful when debugging
- May return its output as a return value (instead of echoing) if the second optional argument is given
For example:
<?php $a = array (1, 2, array ("a", "b", "c")); var_export($a); ?>
which produces:
array ( 0 => 1, 1 => 2, 2 => array ( 0 => 'a', 1 => 'b', 2 => 'c', ), )
Notice the difference between this output and the output for print_r.
For more information about var_export()
, see:
http://php.net/manual/en/function.var-export.php