PHP Echo/Print

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

Even though echo and print statements serve the same purpose, there are certain differences between them. In this article, the reader will be able to demarcate between the application of these two types of output statements. In PHP, two different statements are available to view the output to the browser.

  1. Echo Statement
  2. Print Statement

Read Also: PHP Variables

PHP Echo Statement

We can use an 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 that 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 more than one parameter to echo, the parameters must not enclose within parentheses.

Example 1

This example shows us how to display 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

echo HTML using PHP

Example 3

This example shows us how to display the stored values in variables using 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 display 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 is used as an alternative to echo. 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

echo HTML using PHP

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

print

We can pass multiple strings separated by comma ‘,’ to the echo statement.

We can not pass multiple strings to the echo statement.
The echo statement does not return any value.The print statement always returns 1.
echo is comparatively faster than the print statement.

The print is slower as compared to the echo statement.

Conclusion

Output Statements are the only way for a developer to view the changes made to a code. We hope you have gained a clear understanding of echo/print statements and how to use them to display the output on a web browser.

Do check out further blogs on the other aspects of PHP programming. Check out the previous blogs if you still haven’t.

Leave a Reply

Your email address will not be published. Required fields are marked *