Home >Backend Development >PHP Tutorial >Analysis of the differences between echo, print_r and var_dump in PHP, print_rvar_dump_PHP tutorial
This article provides a more detailed analysis of the differences between echo, print_r and var_dump in PHP. Share it with everyone for your reference. The specific analysis is as follows:
All three are PHP statements with output functions, but print_r(expression) and var_dump(expression) are functions, and echo is just a language structure, not a function, so it cannot be used as part of the expression.
For operating the 8 data types of php:
① echo is used to output numerical variables or strings. But when using echo to output reference variables, such as arrays, only the name of the array is output; when outputting an object, the server prompts
② The function of print_r(expression) is to output an array. In fact, the type of parameter expression can be numeric variables and reference variables .
③ The output result of the var_dump(expression) function is
I hope this article will be helpful to everyone’s PHP programming design.
echo 'Output a string';//Only strings and numbers can be output
print_r — Print easy-to-understand information about variables, generally using the output array structure
var_dump — Print relevant information about variables, generally used for array and object printing
You will know it after using it yourself
Four ways to output strings. echo
print()
printf()
print_r()
echo
can output multiple values at one time, separated by commas . echo is a language construct, not a real function, so it cannot be used as part of an expression.
Correct syntax: echo "Hello", "World";
Wrong syntax: echo ("Hello", "World");
print()
The function print() prints a Value (its argument), returns true if the string was successfully displayed, false otherwise. For example, if (!print("Hello, World")){
die("you are not listening to me");
}
printf()
printf () originates from printf() in C language. This function outputs a formatted string.
Syntax: printf(format,arg1,arg2,arg++)
format specifies the string and how to format the variables in it;
arg1, arg2, ++ and other parameters will be inserted into the main string Semicolon (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.
Example: ?php
$str = "Hello";
$number = 123;
printf("%s world. Day number %u", $str,$number);
?>
#Results======
Hello world. Day number 123
If there are more % symbols than arg parameters, then you must use placeholders. Placeholders are inserted after the % sign and consist of numbers and "\$". See example 3.
Example: ?php
$number = 123;
printf("With 2 decimals: %1\$.2fbr />With no decimals: %1\$u" ,$number);
?>
#Result
With 2 decimals: 123.00
With no decimals: 123
print_r() and var_dump()
print_r() can simply print out strings and numbers, while arrays are displayed as a bracketed list of keys and values, starting with Array. For example, $a = array('name' => 'Fred', 'age' => '15', 'wife' => 'Wilma');
print_r($a);
Output: Array
{
[name] => Fred
[age] => 15
[wife] => ; The same goes for Wilma
}
objects. For example, class P {
var $name = 'nat';
// ...
}
$p = new P;
print_r($p);
Output: Object
{
[name] => nat
}
but print_r( ) The results of outputting Boolean values and NULL are meaningless, because they all print "...the rest of the full text>>