PhP print() Function

What is PHP print() Function?

If you want to output a string or value of a variable, use print() function.

Is print() a Function?

No. print() is not a function, it is a language construct. So, you may not use the parentheses and still you get same result. See example 2.

Syntax:

print(string)

Parameters:

The Function has 1 parameter which is required-

string (Optional): It can be a string, a number, a variable, an expression that can be a value.

Return Values:

The function always returns 1.

Examples:

Example 1:

<?php
print("Hi there. ");
$greetings = "Welcome to PHP";
print($greetings);
?>

Output:

Hi there. Welcome to PHP

Example 2:

<?php
print "Hi there. ";
$greetings = "Welcome to PHP";
print $greetings;
?>

Output:

Hi there. Welcome to PHP

Example 3:

<?php
("A" === "A") ? print 'Equal' : print 'Not equal';
?>

Output:

Equal 

Difference between print and echo:

  • Argument: print takes only one argument, on the other hand, echo can take one or more arguments.
  • Speed: print is slower than echo, as print returns a value.
  • Return Value: print always returns 1, on the other hand, echo returns nothing.
  • Usages in expression: print can be used in an expression as it returns value, on the other hand, echo can be used in an expression. Check example 3.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP print() Function

PHP print is a language construct, not a true function, to display a string or a variable.

Reference:

https://www.php.net/manual/en/function.print.php