Echo and print are both used to print the output data on the screen. The print statement is used as an alternative to echo many times.
PHP Echo/Print Statement
This article will help you to understand how to use echo and print statements in PHP to display the output in a web browser. In PHP, two different statements are available to view the output to the browser.
- Echo Statement
- Print Statement
Read Also: PHP Variables
PHP Echo Statement
We can use echo statement to output one or more strings. Using the echo statement, we can display anything to the browser like strings, numbers, variable values etc. A semicolon (;) identifies the end of the echo statement. The echo is not a function; it is a language construct so we can use it without parentheses.
Syntax
echo "errorsea is the best platform to learn PHP effortlessly"; or echo("errorsea is the best platform to learn PHP effortlessly");
If we want to pass the more than one parameter to echo, the parameters must not enclose within parentheses.
Example 1
This example shows us how to show a string or text with the echo statement.
<?php echo “Hello Suman!”; ?>
Output
Hello Suman!
Example 2
This example shows us how to exhibit the HTML Code with the echo statement.
<?php echo “<h1>My name is Suman</h1>”; echo “<p>How are You?</p>”; ?>
Output
My name is Suman How are You?
Example 3
This example shows us how to exhibit the variable with the echo statement.
<?php $txt = “Hello Suman”; $num = “ 12345“; $colors = array (“Red”,”Green”,”Black”); echo $txt; echo “<br>”; echo $num; echo “<br>”; echo $colors[0]; ?>
Output
Hello Suman 12345 Red
Example 4
This example shows us how to exhibit escaping characters with the echo statement.
<?php Echo “Hello escape \” Sequence\” Characters”; ?>
Output
Hello Escape “Sequence” Characters
PHP Print Statement
The PHP print statement is similar to the echo statement and uses as an alternative to echo sometimes to display output to the browser. Like the echo, the print is also a language construct in PHP, not a function. So we can also use it without parentheses.
Syntax
print "errorsea is the best platform to learn PHP effortlessly"; or print("errorsea is the best platform to learn PHP effortlessly");
Example 1
This example shows us how to exhibit a string of text with the print statement.
<?php print “Hello Suman!”; ?>
Output
Hello Suman!
Example 2
This example shows us how to exhibit the HTML Code with the print statement.
<?php print “<h1>My name is Suman</h1>”; print “<p>How are You?</p>”; ?>
Output
My name is Suman How are You?
Example 3
This example shows us how to exhibit the variable with the echo statement.
<?php $txt = “Hello Suman”; $num = “ 12345“; $colors = array (“Red”,”Green”,”Black”); print $txt; print “<br>”; print $num; print “<br>”; print $colors[0]; ?>
Output
Hello Suman 12345 Red
Read More: PHP Data Types
Difference Between Print and Echo Statements in PHP
echo | |
We can pass multiple strings separated by comma ‘,’ to the echo statement. | We can not pass multiple strings to the echo statement. |
The echo does not return any value. | The print always returns 1. |
The echo is faster than the print statement. | The print is slower compared to the echo statement. |
Conclusion
I hope you have a complete idea of echo/print statements and how to use them to display the output on a web browser.