To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the user of functional cookies. For more information read this page.

Echo and print in PHP

As mentioned before, this site is powered by PHP (version 5 at the moment) and it is powered by echo and print statements. Both of these look like they are exactly the same, but underneath there is a difference.

Output in PHP

Both echo and print serve similar purposes. Both of these statements are designed to output HTML to the page.

For instance, some pages are updated through a seperate file and refreshed using AJAX. The echo and print functions can perform this task.

Data being passed around this way makes for dynamic content which can be processed by the server.

Print

The print function does exactly the same as echo bar the fact that it returns the text from a function. This means that when the system prints, it can then also be assigned to a variable.

PHP
<?php
	//This is an example of print
	print '<p>Hello world!</p>';
?>
						

Echo

The echo function is considered faster than print because it is void, that is it has no return value. The echo function cannot have a variable bound to it. This is an advantage of echo over print, unless it is required to bind some variable to the return value of print (an int that returns whether or not the print was successful or not).

PHP
<?php
	//This is an example of echo
	echo 'Hello world!';
?>