echo, print, print_r differences in PHP
Overview:
All three can output information, but each has its own characteristics:
|
Type |
Number of output variables |
Output Variable Type |
return value |
Speed |
| Echo |
Language structure |
One or more |
Simple type variables, such as int, string |
No |
The fastest |
| Print |
Language structure |
One |
Simple type variables, such as int, string |
Int |
Fast |
| Print_r |
Function |
One |
Complex types, such as arrays, objects |
bool |
Slow |
Echo is not a function, but a PHP statement that can output multiple variables with a comma interval and has no return value, which is also the fastest.
echo "Xyw_", "Eliot", "Blog"; Output Xyw_eliot Blogecho ("Xyw_", "Eliot", "Blog");//Compile error, parentheses cannot pass multiple parameters $name = "Xyw_eliot"; echo "$name is a blog!"; /Output Xyw_eliot is a Blog!echo ' $name is a blog! '; /Output $name is a blog!//double quotation marks will parse the internal variables, output the contents of the variables, and the single quotation marks will not parse the variables, but output them as-is?>
The use of print and Echo is basically the same, but print can only output one variable and has a return value that returns 1 if the output succeeds.
Print "Xyw_eliot is a blog!\n";//Output Xyw_eliot is a blog!//print "Xyw_eliot", "is a blog!"; /Compile error, print cannot pass multiple parameters $return = print "Xyw_eliot is a blog!\n";//print successfully, return 1echo $return;//Output 1?>
Print_r is a function that prints easy-to-understand information about variables. If the argument is a string, integer, or float, the variable value itself is printed. If the parameter is an array, the keys and elements are displayed in a certain format. object is similar to an array.
Print_r has two parameters, the first is a variable, the second can be set to true, and if set to True, returns the variable to be printed, otherwise returns a Boolean value of true.
"Xyw", "2" = "Eliot", "3" = "blog");p Rint_r ($arr);//output array $return =print_r ($arr);//return value is 1echo $return;//Output 1$ Return =print_r ($arr, true);//return array echo $return;//output Array?>
Output:
Array
(
[1] = Xyw
[2] = Eliot
[3] = = Blog
)
This article is Eliot original, reprint please indicate source: http://blog.csdn.net/xyw_blog/article/details/13743341