In PHP, print() returns a Boolean value, while echo has no return value. Use print() to check the output or output multiple expressions at the same time, while echo is suitable for situations where you don't need to check the output or output expressions one by one.
print() and echo in PHP
In PHP, print()
and echo
are both language structures for output data. Despite their similarities, there are subtle differences in usage.
Main difference
The main difference is that print()
returns a Boolean value, while echo
has no return value.
Using
1. print()
true
; if it fails, it returns false
. Syntax:
<code class="php">print($expression1, $expression2, ...);</code>
2. echo
Syntax:
<code class="php">echo $expression;</code>
When to use print() and echo
Use print( ):
Use echo:
Example
<code class="php">// 输出一个字符串 print("Hello world!"); // 返回 true // 同时输出两个表达式 print(5 + 10, "\n"); // 返回 true, 输出 "15" 和换行符 // 输出一个字符串,但不检查结果 echo "This is a message.\n";</code>
Conclusion
While print()
and echo
is a language structure used to output data in PHP, but print()
returns a Boolean value, while echo
has no return value. Use print()
when you need to check the output results or output multiple expressions at the same time; otherwise, use echo
.
The above is the detailed content of The difference between print and echo in php. For more information, please follow other related articles on the PHP Chinese website!