What is the difference between echo and print with examples

What is the difference between echo and print with examples

echo

  1. echo statement is used to display the output. echo can be used with parentheses or without parentheses.
  2. We can pass multiple strings with echo and all of these must separate with commas(,).
  3. echo doesn’t return any value
  4. When we print with echo, the operation is fast as compared to print.

echo statement is used to display the output. echo can be used with parentheses or without parentheses.

Example:

<?php

$name=”shahzeb”;

echo $name;

//or we can echo it as follows;

echo ($name);

?>

Output
shahzeb

We can pass multiple string with echo and all of these must separat with comman(,).

<?php

$name = ” shahzeb “;

$profile = “Programmer”;

$age = 40;

echo $name , $profile , $age, ” years old”;

?>

Output
shahzeb  Programmer  40 years old

echo doesn’t return any value

<?php

$name = ” shahzeb “;

$ret =  echo $name;

?>

Output
Parse error: syntax error, unexpected T_ECHO

  1. echo is faster than print

Print

  1. print statement is used to display the output. print can be used with parentheses or without parentheses.
  2. using print can doesn’t pass multiple arguments
  3. print always return 1
  4. When we print with print, the operation is slow as compared to echo.

Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.

<?php

$name=” shahzeb “;

print $name;

//or we can also print it with echo.

print ($name);

?>

Output
shahzeb

using print can doesn’t pass multiple argument

<?php

$name = ” shahzeb “;

$profile = “programmer”;

$age = 40;

print $name , $profile , $age, ” years old”;

?>

Output
Parse error: syntax error

print always return 1

<?php

$name = ” shahzeb “;

$ret =  print $name;

//To test it returns or not

echo $ret;

?>

Output
shahzeb

it is slower than echo.

Add a Comment