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.
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('php', 'mysql', 'javascript'); $obj2 = (object) array('php', 'mysql', 'javascript'); var_dump($obj1); var_dump($obj2); ?>
Output result:
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.
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('php', 'mysql', 'javascript'); $obj2 = (object) array('php', 'mysql', 'javascript'); echo "<pre class="brush:php;toolbar:false">"; print_r($obj1); print_r($obj2); echo "
Output:
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:
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!