PHP print output a string. It is a language construct not a function. So the use of parentheses is not required with the argument list. Unlike echo, it always returns 1.
Syntax:
int print ( string $arg)
In PHP, print
is a language construct used to output strings or variables to the browser or standard output (in CLI scripts). It behaves similar to echo
, another language construct used for outputting text. However, there are a few differences between print
and echo
:
print
returns a value (1
), whereasecho
does not have a return value.print
can only output one argument, whileecho
can output multiple arguments separated by commas.print
is slightly slower thanecho
because it involves returning a value.
Here’s a basic example of using print
:
$message = "Hello, world!";
print $message;
This would output:
Hello, world!
In practice, echo
is more commonly used due to its simplicity and slightly better performance, but print
can be useful in certain situations where you need to explicitly evaluate its return value.