What is “print” in PHP?

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:

  1. print returns a value (1), whereas echo does not have a return value.
  2. print can only output one argument, while echo can output multiple arguments separated by commas.
  3. print is slightly slower than echo because it involves returning a value.

Here’s a basic example of using print:

php
<?php
$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.