Difference between print and echo in PHP with examples

We commonly use two types of function to show output on screen in PHP. They are echo and print. But here are some differences among using these. Let us describe these here.


  • Both of them are using to show the output.
For example run the following code.

<?php 


echo "Hi<hr/>";

print "Hi<hr/>";

?>
  • They also can execute variables using double quote.
I have declared a variable here. Check carefully.

<?php 

$var = ' my variable';

echo "Hi$var<hr/>";

print "Hi$var<hr/>";

?>
  • We also can assign multiple statements in one echo or print function..

<?php 

$var = ' my variable';

echo "Hi".$var."<hr/>";

print "Hi".$var."<hr/>";

?>
  • We can use multiple parameters in echo but it is not supported in print.

<?php 

echo "This ", "string ", "was ", "made ", "with multiple parameters.<hr/>";

?>
  • Both of them can do mathematical operations..
For example:

<?php 

$x = 10;
$y = 11;

echo $x + $y;
print $x + $y;


?>


Hence here is a little difference between them. But it is also noted that the print function can operate more quickly than echo function. Simply echo is idle.

Happy PHP programming.

Related Posts
Previous
« Prev Post