You will notice as soon as you start programming in PHP that you have two options when it comes to displaying text on the screen: Echo or Print. Both examples shown below will display exactly the same text on the screen in exactly the same way. So… which one should you use?
<?php
print "Hello World!";
?>
<?php
echo "Hello World!";
?>
There is one small difference between print and echo. With print you can return a true or false value which means you can use it as part of a more complex expression. Because the print function gives you this extra functionality which may be of use, I would suggest simply using print all the time.
Of course, because echo does not return a value, it can be argued that echo is faster than print. This is true, though the difference in execution time is so small that in practice it is negligible. In the past, it may have been possible to speed up a large PHP application by several milliseconds by converting all prints to echos, but with the speed of today’s computers I doubt there would be any useful performance improvement. If you need to improve the performance of your program, optimizing your algorithms will be way more beneficial than switching to echo.
At the end of the day, it’s really up to the preference of the programmer when it comes to choosing between Echo and Print. As I’ve mentioned, I’ve gone with only using Print because I feel that my code is more easy to understand and therefore of higher quality this way than if I began mixing both Echo and Print. If I just used echo, I may still need to use Print in some cases should I need to return a value, therefore I would inevitably find myself mixing both functions and confusing any new PHP programmers I share my code with! Also, the print command is more common in other programming languages and is more intuitive which are some other reasons I have decide to stick with just using Print.
Here is an example of print in action:
<html>
<head>
<title>PHP Page with Print in Action</title>
</head>
<body>
<?php
print "This text will be displayed!";
print "<br>";
print "The line above used HTML!";
print "This \"is how we can use\" quotes";
?>
</body>
</html>