PHP Tutorial

echo and print in PHP: Difference between echo and print

echo and print in php

echo and print both are used to print string on the screen. echo is little bit different from print because print always return 1. While, echo doesn’t return any type of value.

echo and print both do the same job but a little bit different from each other. echo is used to display output on the screen and it doesn’t return any value while print is also used to display output on the screen it always returns a value like 1.

Most of the people say that echo and print are functions but according to php.net description, both are not functions because we can use them without parenthesis. Parenthesis is not required to use them.

In this tutorial, we will learn what is echo? What is print? Why both are not a function and what is the difference between echo and print? Let’s see each topic one by one.

Difference between echo and print in PHP

echoprint
echo is used to display string output on the screen and it accepts single and multiple both types of data. Multiple data is separated by a comma.print is also used to display string output on the screen and it only accepts single data.
echo doesn’t return any type of value.print always return 1.
echo is faster than print because it does not return any value.print is slower than echo because it always returns 1.

echo in PHP

  • echo is a language construct not a function that’s why we can use it with parenthesis or without parenthesis.
  • echo is used to display output as a string on the screen.
  • echo is a little bit faster than print.
  • echo doesn’t return any value.
  • echo accepts single data as well as multiple data. Multiple data is separated by a comma. Multiple data will always pass without parenthesis. With parenthesis, you cannot pass multiple data.
<?php

//print single sentence
echo "This is a single sentennce";

//accept multiple data separated by comma
echo "This is programming blog.", "This is a tutorial blog";

//print variable
$name = "PHPTreePoint";
echo $name;

//print escaping character
echo "Hello escape \"sequence\" characters";  

//print with parenthesis
echo("This is a man");

//it will give error
echo("This is a man", "test second parameter");

print in PHP

  • print is a language construct, not a function that’s why we can use it with parenthesis or without parenthesis.
  • print always return 1 as integer value.
  • print takes only single data or parameter. It doesn’t accept multiple data as echo.
  • It is slower than echo because it always return a value.
<?php

//print single sentence
print "This is a single sentennce";

//print variable
$name = "PHPTreePoint";
print $name;

//print escaping character
print "Hello escape \"sequence\" characters";  

//print with parenthesis
print("Hi This is a man");


//it will give error
print "This is programming blog.", "This is a tutorial blog";

Why echo and print are not functions?

Now, the most important question is why echo and print are not functions? Why is it said that both are language constructs?

First, we will understand what is a language construct? A language construct is a part of programming language that is created to perform a specific task of operation.

Somewhere, we can say all keywords of a programming language are language construct.

Now, we will understand why echo and print in PHP are not functions? As we already know that functions are a group of the statement which is used to perform a special task and functions must have parenthesis but in case of echo and print, we can use it without parenthesis that’s why it is not a function.

Another reason is; we can use the function as a callback but we can’t use echo and print as a callback function that’s why both are not functions.

Why should I use a language construct instead of a function?

Most of the time, we use functions instead of language construct while language construct is available for the same task.

We will describe to you why should we use a language construct instead of functions.

As we already discussed that keywords are also language constructs. In every programming language, a compiler or parser is used to parse or convert human-readable code into binary code that means computer-readable code.

The compiler or parser knows their keyword syntax but they don’t know there functions syntax that’s why they need to call their function to their respective location to get the result and this is the reason language construct is faster than functions.

That’s why we should use language construct instead of functions if possible.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.