Echo and Print Statements

In PHP there are two basic ways to get output: echo and print. The major difference between them is, echo returns no value while print statement returns 1, so it can be used in the expressions.

In this tutorial we use echo (and print) in almost every example. So, this article contains a little more info about those two output statements.

It’s really a regular doubt to know the difference between echo and print in PHP. This PHP Tutorials lesson helps you to avoid confusion between Echo() and Print() difference.

It’s very simple to understand but its a great confusion to understand difference between print() and echo() statements in PHP. I have given a clear picture to understand the basic difference between echo() and print() in PHP.

Outputs all parameters. No additional newline is appended.

echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in this context. Additionally, if you want to pass more than one parameter to echo, it must not be enclosed within parentheses.

echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals (=) sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.

PHP echo and print Statements

There are some differences between echo and print:

  • echo – can output one or more strings
  • print – can only output one string, and returns always 1

Tip: echo is marginally faster compared to print as echo does not return any value.

Basic example to use print in PHP : 

<?php
$title = "PHP Tutorial";
print "Welcome to $title";
?>
<?php
print "Welcome to PHP Tutorial";
?>

Output: Welcome to PHP Tutorials.

Note: Although the official syntax calls for the use of parentheses to enclose the argument. They are not required because print() is not technically a function as well. It is a said to be a language construct (like echo).

PHP print() statement’s return value is misleading because it will always returns 1 regardless of the output. It is somewhat different from most other functions in PHP return value often serves as an indicator of whether the function executed as intended.

The echo() Statement in PHP :

There is another identical function name echo(). There are various difference between echo() and print(). See, the following is the basic structure of echo() in PHP.

void echo(string arg1, arg2….)

The echo() in PHP is capable of outputting multiple strings here. The utility of this particular trait is questionable; using it seems to be a matter of preference more than anything else.

<?php
$author = "Nohman":;
$website = "PHP Tutorials";
echo "Author of".$website."is: ".$author;
?>

The main difference between echo() and print() in PHP is that echo() has no return value where print() has a return value 1.
The echo() in PHP takes multiple parameters where as print() takes only a single parameter.
Conceptually echo() is faster than print() in PHP.

<?php
print "<h1>PHP print statement test</h1>";
print "This is a PHP print statement<br>";
print "A new PHP print statement arrived";
?>
<?php
$var1 = "Visit";
$var2 = "PHP Tutorials";
$x = 80;
$y = 40;
echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2";
echo $x+$y;
?>

Add a Comment

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

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote