Output one or more values (String), separated by commas. echo is a language construct, not a real function, so it cannot be used as part of an expression.
The function print() prints a value (its parameter) if the string is successful Returns true if displayed, false otherwise. Same as echo, but slower than echo.
You can simply print out strings and numbers. If a string, integer or float is given, it will Print the variable value itself. object is similar to an array. Arrays, on the other hand, are displayed as a bracketed list of keys and values, starting with Array. Remember, print_r() will move the array pointer to the end. Use reset() to return the pointer to the beginning.
$arr = array('name'=> 'bob','age' => 20, 'sex' => 'man'); print_r($arr);
The output is in the form:
Array{ [name] => bob, [age] => 20, [sex] => man }
But yes, it is meaningless to use print_r() to display Boolean values and NULL:
For example:
print_r(true); //输出1 print_r(false); //没有输出 print_r(null); //没有输出
The results of print_r() outputting Boolean values and NULL are meaningless, because they all print "\n". Therefore, using the var_dump() function is more suitable for debugging.
Determine the type and length of a variable, and output the value of the variable. If the variable has a value, the output is the variable value and returns data type. This function displays structural information about one or more expressions, including the expression's type and value. Arrays will recursively expand values, showing their structure via indentation.
var_dump(true); //输出 bool(true) var_dump(false); // bool(false) var_dump(null); // bool(null) var_dump(array('name' => 'bob', 'age' => 20)); array(2) { ['name'] => string(3) 'bob' ['age'] => int(20) }
Similar to print_r and var_dump, but rarely used.
Var_dump returns the type and value of the expression while print_r only returns the result. Compared with debugging code, using var_dump is more convenient. Easy to read.
The differences between print_r(), var_export() and var_dump() are as follows:
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') ); echo "<pre class="brush:php;toolbar:false">"; print_r($arr);echo "";echo "
"; var_export($arr);echo "";echo "
"; var_dump($arr);echo "";
The output result of print_r($arr)
Array( [0] => Array ( [a] => aa [b] => bbb [c] => ccc ) [1] => Array ( [a] => ddd [b] => eee [c] => fff ) [2] => Array ( [a] => gg [b] => hh ) )
The output result of var_export($arr)
array ( 0 => array ( 'a' => 'aa', 'b' => 'bbb', 'c' => 'ccc', ), 1 => array ( 'a' => 'ddd', 'b' => 'eee', 'c' => 'fff', ), 2 => array ( 'a' => 'gg', 'b' => 'hh', ), )
The output result of var_dump($arr)
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)
The following is another output in json format:
$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);echo "<pre class="brush:php;toolbar:false">"; print_r($arra);echo "";echo "
"; var_export($arra)echo "";echo "
"; var_dump($arra);echo "";
print_r($arra) output
[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]
var_export($arra) output
'[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]'
var_dump($arra)output
string '[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]' (length=84)
The above is the detailed content of Detailed introduction to the differences between echo print() print_r() var_dump() in PHP during debugging. For more information, please follow other related articles on the PHP Chinese website!