Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 2.3Echo and print in PHP

Part 2.3Echo 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 there world!';
?>
				
Feedback 👍
Comments are sent via email to me.