Echo and Print both are the php statement. They are used to display the output on the screen. Both the statements are used in a different manner to get the output which we will discuss later in this article.
But first let us give you some information about both the statements. So we first start with echo statement. Join our php training program at your nearby Location in India and understand with help of professional instructors.
ECHO STATEMENT
Syntax : echo “text”;
➡ Echo is a php statement used to display output on the screen. It can be used either with parentheses or without parentheses. For instance, echo or echo( ).
➡ It can be used to pass multiple arguments.
➡ Echo doesn’t return any value.
➡ Echo is much faster as compared to print statement
Example of Echo Statement :
An example is shown below using multiple parameters without parentheses.
You will get the following output from the above PHP Code :
Welcome to the world Of PHP !
I’m about to Teach You PHP !
This is the example with multiple parameters.
Description: In the first statement we have used <h2> to get output in the bold characters which is the main heading. The second statement is written in simple form. And in the third statement we have used more than one parameter in which each parameter is separated by a comma.
Another Example :
An example is shown below using variables.
You will get the following output from the above PHP Code :
Learn PHP
Study PHP at W3Training Schools
2
Description : Here we have used four different variables $txt1, $txt2, $x, $y with values “Learn PHP”, “W3Training Schools”, “1” and “1” respectively. To print the output, these values are declared inside the echo statement. We will get the output of first statement in bold characters as we have used <h2> heading. The output of second statement will be “Study PHP at W3Training Schools”. In the third statement, we added the values of third and fourth variable. So the output of this will be 2.
PRINT STATEMENT
Syntax : print “text”;
➡ Print is also a php statement used to display the output on the screen. Like echo statement, it can also be used either with parentheses or without parentheses. For instance, print or print( )
➡ Print statement cannot pass multiple argument.
➡ Print always return 1.
➡ It is comparatively slower.
Example of Print Statement :
You will get the following output from the above PHP Code :
Welcome to the world Of PHP
Hello world !
I’m about to Teach You PHP !
Description : This is a simple example. You just have to declare value inside the print statement as shown above and you will get the desired output on your screen.
Another Example:
You will get the following output from the above PHP Code using print statement
Learn PHP
Study PHP at W3Training Schools
4
Description : In this example four variables are used namely $txt1, $txt2, $x, $y having values “Learn PHP”, “W3Training Schools”, “2” and “2” respectively. After declaring these values inside the print statement we will get the desired output. The output of first print statement will be in bold characters as we have used <h2> heading. The output of second print statement will be “Study PHP at W3Training Schools”. And the third output we will be getting is 4 as we have added the values of x and y in the third print statement.
CONCLUSION
There are only two or three differences between the echo and print statement. Echo is faster while print is comparatively slow. Echo doesn’t return any value while print always returns 1. One can use multiple arguments in echo statement while print statement can only pass a single argument.