In PHP, echo
and print
are both used to output data to the browser. However, there are a few differences between them:
1. echo
is slightly faster than print
, because it doesn’t return a value and doesn’t require a function call.
2. echo
can output multiple strings separated by commas, while print
can only output a single string.
For example, you can use echo
like this:
echo "Hello, ", "world!";
But you cannot use print
in the same way:
print "Hello, ", "world!"; // syntax error
3. print
always returns 1
, while echo
does not return a value.
Here is an example of how to use print
:
$result = print "Hello, world!";
echo $result; // output: 1
In general, echo
is more commonly used, because it is faster and more flexible. However, print
can be useful in certain situations, such as when you need to use it as an expression or when you want to output a value that can be tested in an if statement.
if (print "Hello, world!") {
// this code will always be executed
}