What is difference between echo and print?

Last updated 2 years, 8 months ago | 513 views 75     5

Tags:- PHP

PHP |What is the difference between echo and print?

PHP echo and print both are used to display the output in PHP. Both can be used with or without parenthesis. The differences are small

  • echo has no return value while print has a return value of 1, so it can ve used in expressions.
  • echo can take multiple parameters where as print can take only one argument.
  • echo is marginally faster than print.
 
<?php

$name="John";

// without parenthese
echo $name;
print $name;

//with parenthese       
echo ($name);
print ($name);

?>

echo has no return value while print has a return value of 1, so it can ve used in expressions.

<?php

$name="John";

$check =  echo $name;
echo $check; // throw a error message of Parse/syntax error

$check =  print $name;

//To test it returns or not
echo $check; //Output will be John

?>

echo can take multiple parameters whereas print can take only one argument.

<?php

$name = "John";
$profile = "PHP Developer";
$age = 25;

echo $name , $profile , $age, " years old";  //Output will be John PHP Developer 25 years old

print $name , $profile , $age, " years old"; // throw a error message (Parse error : syntax error)

?>