Detailed introduction to the differences between echo print() print_r() var_dump() in PHP during debugging

黄舟
Release: 2023-03-07 06:26:01
Original
1822 people have browsed it

echo

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.

print()

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.

print_r()

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);
Copy after login

The output is in the form:

Array{
     [name] => bob,

     [age] => 20,

     [sex] => man
}
Copy after login

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);          //没有输出
Copy after login

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.

var_dump()

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)
}
Copy after login

var_export()

Similar to print_r and var_dump, but rarely used.

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. 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 "
";
Copy after login

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
        )

)
Copy after login

The output result of var_export($arr)

array (  0 => 
  array (    &#39;a&#39; => &#39;aa&#39;,    &#39;b&#39; => &#39;bbb&#39;,    &#39;c&#39; => &#39;ccc&#39;,
  ),  1 => 
  array (    &#39;a&#39; => &#39;ddd&#39;,    &#39;b&#39; => &#39;eee&#39;,    &#39;c&#39; => &#39;fff&#39;,
  ),  2 => 
  array (    &#39;a&#39; => &#39;gg&#39;,    &#39;b&#39; => &#39;hh&#39;,
  ),
)
Copy after login

The output result of var_dump($arr)

array (size=3)  0 => 
    array (size=3)    &#39;a&#39; => string &#39;aa&#39; (length=2)     &#39;b&#39; => string &#39;bbb&#39; (length=3)     &#39;c&#39; => string &#39;ccc&#39; (length=3)  1 => 
    array (size=3)    &#39;a&#39; => string &#39;ddd&#39; (length=3)  &#39;b&#39; => string &#39;eee&#39; (length=3)   &#39;c&#39; => string &#39;fff&#39; (length=3)  2 => 
    array (size=2)    &#39;a&#39; => string &#39;gg&#39; (length=2)     &#39;b&#39; => string &#39;hh&#39; (length=2)
Copy after login

The following is another output in json format:

$arr=array(array(&#39;a&#39;=>&#39;aa&#39;,&#39;b&#39;=>&#39;bbb&#39;,&#39;c&#39;=>&#39;ccc&#39;),           
array(&#39;a&#39;=>&#39;ddd&#39;,&#39;b&#39;=>&#39;eee&#39;,&#39;c&#39;=>&#39;fff&#39;),           
array(&#39;a&#39;=>&#39;gg&#39;,&#39;b&#39;=>&#39;hh&#39;));
$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 "
";
Copy after login

print_r($arra) output

[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]
Copy after login

var_export($arra) output

'[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]'
Copy after login

var_dump($arra)output

string '[{"a":"aa","b":"bbb","c":"ccc"},{"a":"ddd","b":"eee","c":"fff"},{"a":"gg","b":"hh"}]' (length=84)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template