This article mainly introduces the difference between print_r and var_dump when testing PHP code. Interested friends can refer to it. I hope it will be helpful to everyone.
Speaking of output, I have to mention printing in php.
The first is of course the most commonly used echo, echo: output one or more strings;
print: the same as echo, but slower than echo.
print_r:
Print easy-to-understand information about the variable. If a string, integer, or float is given, the variable value itself will be printed. If an array is given, the keys and elements will be displayed in a certain format. object is similar to an array. Remember, print_r() will move the array pointer to the end. Use reset() to return the pointer to the beginning.
var_export: Similar to print_r and var_dump, but rarely used.
var_dump:
This function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values recursively, showing their structure through indentation.
The difference between var_dump and print_r:
var_dump returns the type and value of the expression while print_r only returns the result. It is easier to read than using var_dump to debug code.
The differences between print_r(), var_export() and var_dump() are as follows:
eg: Output of two-dimensional array:
$arr=array(array('a'=>'aa','b'=>'bbb','c'=>'ccc'), array('a'=>'ddd','b'=>'eee','c'=>'fff'), array('a'=>'gg','b'=>'hh')); print_r($arr); //print_r输出: //Array ( [0] => Array ( [a] => aa [b] => bbb [c] => ccc ) [1] => Array ( [a] => ddd [b] => eee [c] => fff ) [2] => Array ( [a] => gg [b] => hh ) ) var_export($arr); //var_export输出: //array ( 0 => array ( 'a' => 'aa', 'b' => 'bbb', 'c' => 'ccc', ), 1 => array ( 'a' => 'ddd', 'b' => 'eee', 'c' => 'fff', ), 2 => array ( 'a' => 'gg', 'b' => 'hh', ), ) var_dump($arr); //var_dump输出: /* array (size=3) 0 => array (size=3) 'a' => string 'aa' (length=2) 'b' => string 'bbb' (length=3) 'c' => string 'ccc' (length=3) 1 => array (size=3) 'a' => string 'ddd' (length=3) 'b' => string 'eee' (length=3) 'c' => string 'fff' (length=3) 2 => array (size=2) 'a' => string 'gg' (length=2) 'b' => string 'hh' (length=2) */
A json is given below Format output:
$arr=array(array('a'=>'aa','b'=>'bbb','c'=>'ccc'), array('a'=>'ddd','b'=>'eee','c'=>'fff'), array('a'=>'gg','b'=>'hh')); $arra=json_encode($arr); print_r($arra); //print_r输出: [{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}] var_export($arra); //var_export输出: '[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]' var_dump($arra); //var_dump输出: string '[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]' (length=84)
I think everyone can understand the specific difference at a glance
Related recommendations:
php printing function introductory tutorial
Detailed introduction to PHP printing output function summary
The detailed explanation is corrected in PHP prints the error of the current page
The above is the detailed content of The difference between print_r and var_dump when testing PHP code. For more information, please follow other related articles on the PHP Chinese website!