echo and print statements in PHP

difference between echo and print statements in PHP.

echo and print both statements are use to display output result on screen.

echo and print statements in php both are same.
There is a small minor difference between echo and print statements.

The echo Statement :

  • The echo statement does not return value.
  • echo statement can accept multiple expressions.
  • echo statement faster than print because of it has no any return value.
  • echo can accept multiple string separate by comma ( , ).

 The print Statement :

  • The print statement always return the value 1.
  • print statement cannot accept multiple expressions.
  • print statement slower than echo statement because it has a return value.
  • print statement can not accept multiple arguments.

Let’s take some php example to understand about echo and print statements in detail.


PHP echo statement

The echo statement is used to display output result on screen.

The echo statement Example:

Simple echo example to display result.

<?php

$myvar = “meera academy”;
echo $myvar;

// OR

echo ($myvar);
?>

The output is : meera academy
The “echo” and “echo()” both are produce same output on above example.

We already talked above that the echo statement has no return type.

echo example with return type

<?php

$myvar = “meera academy”;
$newvar = echo $myvar;

echo $newvar;

?>

The output is :
Parse error: syntax error, unexpected ‘echo’ (T_ECHO)

In above return type php example we have return type echo with $myvar variable with value “meera academy”. When run above code it shows error like : “Parse error: syntax error, unexpected ‘echo’ (T_ECHO)”.

echo example with multiple arguments

<?php

$myvar = “meera academy “;
$firstvar =”PHP training center”;

echo $myvar . $firstvar;

?>

The output is :
meera academy PHP training center

In above echo multiple argument pass example we have declare two string variable $myvar and $firstvar with value “meera academy ” and “PHP training center” respectively.
Here, we print two variable together using echo statement.
For join two string together php use comma ( , ) or dot ( . ).


PHP print statement

It is used to print output on screen same as echo.

Simple print example to display result on screen in php.

<?php

$myvar = “meera academy”;
print $myvar;

// OR

print ($myvar);
?>

The output is : meera academy
The “print” and “print()” both are produce same output on above example.

print example with return type

<?php

$myvar = “meera academy”;
$newvar = print $myvar;

print $newvar;

?>

The output is :
meeraacademy1

We already know that the print statement has return value 1.
In above return type php print example we have return type print with $myvar variable with value “meera academy”. When run above code it output value with return type = 1.

echo example with multiple arguments

<?php

$myvar = “meera academy “;
$firstvar =”PHP training center”;

print $myvar . $firstvar;

?>

The output is :
Parse error: syntax error

In above print multiple argument pass example we have declare two string variable $myvar and $firstvar with value “meera academy ” and “PHP training center” respectively.
Here, we will get syntax error as we know print does not pass multiple arguments.

Leave a Reply

Your email address will not be published. Required fields are marked *

8 + 7 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">