PHP echo and print statements
Difference between echo and print:
echo - Able to output more than one string
print - Can only output one string and always returns 1
Tip: echo is slightly faster than print because it does not return any value .
PHP echo statement
echo is a language structure that can be used with or without parentheses: echo or echo().
PHP print statement
print is also a language structure and can be used with or without parentheses: print or print().
Common output statements
echo()
You 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.
print()
The function print() prints a value (its parameter) and returns true if the string is successfully displayed, otherwise it returns false.
print_r()
Strings and numbers can be simply printed out, while arrays are displayed as a bracketed list of keys and values, starting with Array. But 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 value of the variable is output and the data type is returned. This function displays structural information about one or more expressions, including the expression's type and value. Arrays will expand values recursively, showing their structure through indentation.
The result of using echo is:
The result of using print() is:
The output result of using print_r() is:
The output result of using var_dump() is:
Recommended tutorial: PHP video tutorial
The above is the detailed content of Differences in php output statements. For more information, please follow other related articles on the PHP Chinese website!