Home  >  Article  >  Backend Development  >  The difference between var_dump() and print_r() in php

The difference between var_dump() and print_r() in php

青灯夜游
青灯夜游Original
2018-12-28 17:21:518676browse

What is the difference between var_dump() and print_r() in php? This article will give you a brief comparison between var_dump() and print_r(), so that you can understand the difference between var_dump() and print_r(). I hope it will be helpful to you.

The difference between var_dump() and print_r() in php

var_dump() function

The var_dump() function displays the structured information of the variable, including Its data type, value, length, and number of elements. It is used for debugging code and has no return value.

Example 1: There is an array obj1 and an object obj2, use the var_dump() function to output

<?php  
$obj1 = array(&#39;php&#39;, &#39;mysql&#39;, &#39;javascript&#39;);
$obj2 = (object) array(&#39;php&#39;, &#39;mysql&#39;, &#39;javascript&#39;);
var_dump($obj1);
var_dump($obj2);
?>

Output result:

The difference between var_dump() and print_r() in php

Description:

The information dumped by the var_dump() function is automatically included in the pre element, and each type seen has its own color. Enhance readability.

The difference between var_dump() and print_r() in php

Example 2: If you enter

var_dump(null);

it returns NULL, var_dump() is mainly used for debugging.

print_r() function

The print_r() function displays variable-related information in a concise and easy-to-read manner. Arrays will be displayed in key and value format, but element data types cannot be displayed.

The print_r() function can take two parameters, the first parameter is the variable to be dumped, and the second parameter is a Boolean value. When we set the second parameter to true, no value can be returned, only the value can be dumped/outputted.

Example 1: There is also an array obj1 and an object obj2, use the print_() function to output

<?php  
$obj1 = array(&#39;php&#39;, &#39;mysql&#39;, &#39;javascript&#39;);
$obj2 = (object) array(&#39;php&#39;, &#39;mysql&#39;, &#39;javascript&#39;);
echo "<pre class="brush:php;toolbar:false">";
print_r($obj1);
print_r($obj2);
echo "
"; ?>

Output:

The difference between var_dump() and print_r() in php

print_r The information dumped by the function will not be automatically included in the pre element. We must add the information to the pre element ourselves, otherwise it will be displayed on one line:

The difference between var_dump() and print_r() in php

Example 2: If you enter

print_r(null)

no value will be returned.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of The difference between var_dump() and print_r() in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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