PHP Echo Vs Print Statements

In this tutorial, we will compare PHP echo with PHP print statement.


We have seen both echo and print statements in our previous tutorials. Both of these statements are used frequently to display the output. In this tutorial let’s see a comparison between the two statements:

  • PHP echo
  • PHP print

In PHP echo and PHP print both languages are construct. They never behave like a function. Therefore no parentheses required. However both echo and print can use with or without parentheses. We can use these statements to get output strings and variables.

Comparison between echo and print

“echo” “print”
Uses To print output of string, variable This is also used to print output of string, variable
Syntax void echo(string $arg1 [, string $… ]) int print(string $arg)
Return Does not return anything Always return 1
String  It can take one or more strings. It can take only one string at a time
Speed echo is faster than print print is slower than echo
Parentheses Can use with or without parentheses Can use with or without parentheses

You can also check the difference between PHP echo and PHP print with the help of the following programs.

PHP echo Vs print: Checking Multiple arguments

We can pass multiple arguments in PHP echo separated by a comma. This will not give any syntax error.

<?php 
$msg1 = "Hello "; 
$msg2 = "World!"; 
echo "The Message is: ".$msg1,$msg2; 
?>

Output

The message is: Hello World!

However, this will give an error in the PHP print statement as the print statement not accept multiple arguments.

<?php 
$msg1 = "Hello "; 
$msg2 = "World!"; 
print "Message is: ".$msg1,$msg2; 
?>

Output 

Parse error: syntax error, unexpected ‘,’ in C:\xampp\htdocs\p2.php on line 4

PHP echo vs print: Check Return value

<?php 
$msg1 = "Hello "; 
$msg2 = "World!"; 
$ret = echo "Message is: ".$msg1,$msg2;
echo "</br>"; 
echo "Value return by print statement: ".$ret; 
?>

Output 

Parse error: syntax error, unexpected ‘echo’ (T_ECHO) in C:\xampp\htdocs\p2.php on line 4

As we know the PHP print statement always returns 1.

<?php 
$msg = "PHP"; 
$ret = print "The Message is: ".$msg;
print "</br>"; 
print "Value return by print statement: ".$ret; 
?>

Output

The Message is: PHP
Value return by print statement: 1

Please get connected & share!

Advertisement