Saturday, June 6, 2015

echo vs. print in PHP




echo() and print() are not functions but language constructs in PHP. They are both used to output strings and there are very minor differences between echo and print in PHP.

Comparison chart

Echo (PHP)

Print (PHP)

Parameters echo can take more than one parameter when used without parentheses. The syntax is echo expression [, expression[, expression] ... ]. Note that echo ($arg1,$arg2) is invalid. print only takes one parameter.
Return value echo does not return any value print always returns 1 (integer)
Syntax void echo ( string $arg1 [, string $... ] ) int print ( string $arg )
What is it? In PHP, echo is not a function but a language construct. In PHP, print is not a really function but a language construct. However, it behaves like a function in that it returns a value.

Speed of echo vs print in PHP

The speed of both echo and print statements in PHP is roughly the same. Using one over the other is not likely to yield any performance improvement in your application. Theoretically, echo is more efficient because it does not return any value.

Function vs Language Construct

Unlike most PHP string functions, echo and print aren't functions but language constructs. Therefore it is not required to use parentheses when using echo or print.

Parameters and syntax of print vs echo

When used with parentheses (like a function call), both print and echo take only 1 argument. For example,
echo("Don't panic!");
However, when used without parentheses, echo can take several arguments. For example,
echo "Don't", " ", "panic", "!";

A blogger

I am passionate blogger cum B.Tech. Computer engineering graduate. I love writing blog post. I spend my free time in writing blog post that will useful to everyone (including me). I have had some success making money blogging and want to help others do the same. I just figured that by creating a great and free resource a lot of links would follow – and they have. Some people ask me how they can repay me – which is not necessary - but for those wanting to show their appreciation, I just say linking to the article from their blog is the best compensation I could receive. Thanks for reading!

0 comments:

Post a Comment

Thanks for commenting. I will Reply you soon

 

Copyright @ 2013 PHP WORDPRESS MAGENTO .

Designed by Kcon Technosoft