Home » PHP » PHP echo and print

PHP echo and print

We have used 'echo()' language construct in the tutorials of PHP and now we will elaborate it. In the heading, there is another contruct listed which is print, what it is and how is it connected to echo? PHP Print and PHP Echo are both language constructs used to display output on the screen, but then why there are two language constructs doing the same job? Well there are certain differences also and as per the demand of the situation we use any one of them. Let's go through both the language constructs one by one to make it easy to understand:

Language Construct

A Language Construct is similar to a PHP function in a programming language but with some differences. In terms of syntax a function will always use parenthesis(), but a language construct doesn't necessarily requires it. Print and Echo are Language Constructs in PHP, there are many more and there are other differences also between a function and a Construct but it is a bit deeper and we will talk about it later. For now just keep it in mind that these both are 'Language Constructs' and not 'functions' or 'statements'. Let's continue with Echo!


The PHP ECHO construct is used in PHP to display content on the screen, i.e. to display output. It can be used to display a normal string of characters, variable's value, any expression's result(like $a+$b), etc.

As we have told you earlier that because Echo is a language construct and really an in-built function so it can be used with or without parenthesis(). But there is a drawback of using parenthesis that you can only use single parameter with it, if you want to display multiple parameters' values then you have to use those within double quotes ("").

How to display String and variables with ECHO?

String can be displayed by enclosing it with single('') or double quotes(""). Both type of quotes will give different results when using them with varibales. Single quotes('') will treat variables as string and instead of displaying their value, variables name will be displayed as written. But if you use double quotes("") then variable's values will be displayed with the string.

For variables, you can use parenthesis or can choose not to use it at all. For directly displaying expressions(say, $x+$y) value, you can choose to enclose the expression within parenthesis or can choose not to, both will give the same result. See the example below and look at all the possible ways to use PHP ECHO.


<?php $x=4; $y=10; $hello="Hello PHP!"; echo ($y); echo "<br/>"; echo "$x"; echo "<br/>"; echo '$x'; echo "<br/>"; echo $x+$y; echo "<br/>"; echo "$x $y"; echo "<br/>"; echo "$x and $y"; echo "<br/>"; echo '$x and $y'; echo "<h2>PHP is Fun!</h2>"; echo "$hello<br/>"; echo "This ", "string ", "is ", "made ", "with multiple parameters."; ?>


4 10
4 and 10
$x and $y

PHP is Fun!

Hello PHP!
This string is made with multiple parameters.

You can see in the example above, how you can display string using ECHO and combine strings with variables. We hope you have understood the difference between using different quotes and how you can store and display strings using variables with echo.

PHP PRINT Statements

PHP PRINT construct is same as echo but slightly different and slow. It is also a Language Construct so you can use it with or without parenthesis().

The difference between ECHO and PRINT is that PRINT always returns '1' which can be used in any expression but echo doesn't return any value and that's why ECHO is slightly faster.

How to display String and variables with PRINT? In case of PRINT, we cannot pass multiple parameters at once unlike ECHO, for each parameter we has to write another PRINT() Construct. Also in ECHO we used comma(,) to separate strings but we cannot do it with PRINT().

For variables, you can enclose them with parenthesis or double quotes but if you want to use a variable's value with a string then you have to dot(.) operator. Also to directly dispaly an output of any expression(say, $a+$b) you can choose to enclose the expression with parenthesis or you can also choose not to, it will work. Look at the example below to see all the possible ways of displaying output with PHP PRINT:


<?php print "< h2>Hello PHP< /h2>"; print "Hello world!< br>"; print "I'm about to learn PHP!"; ?>


Hello PHP

Hello world!
I'm about to learn PHP!

Let's take one more example:


<?php print "< h2>Hello PHP< /h2>"; print "Hello world!< br>"; print "I'm about to learn PHP!"; ?>


Hello PHP

Hello world!
I'm about to learn PHP!


<!DOCTYPE html> <html> <body> <?php $txt1 = "Learn PHP"; $txt2 = "CodeRepublics.com"; $x = 15; $y = 20; print "<h2>" . $txt1 . "</h2>"; print "Study PHP at " . $txt2 . "<br>"; print $x + $y; ?> </body> </html>


Learn PHP

Study PHP at CodeRepublics.com

Comparison between Echo and Print in PHP:

Difference PHP Echo PHP Print
type It is a Language Construct. It is also a Language Construct.
Parenthesis It can or can't be written with parenthesis. It can or can't be written with Parenthesis.
Strings Can take multiple strings at a time. Can only take one string at a time.
Speed It is faster. Slower than ECHO.
Return Value It doesn't return any value. It returns 1 always.

Color Picker

List of color names supported by all browser »

Character Code

List of character codes to display special character »

Language Code

List of all language supported by all browser »

Country Code

List of Country code supported by all browser »