The biggest triviality that plagues most newcomers to PHP is the fact that there are two commands that do (what appears to be) the exact same thing. Print and Echo both output data to the screen in a similar fashion- so why have two different commands for the same thing?
PHP Echo Vs Print Diagram
In all actuality, Echo and Print differ based on how they are structured. Print returns a value much like a normal function would. But despite common belief, Print is not a function, as we can see by the fact that it doesn’t require parenthesis to work (Not to be confused with Printf). Print and Echo are actually both called language constructs, although this isn’t to say that we can’t make Print act like a function.
PHP Echo Vs Print: Which Is Faster?
Developers need to ask the all-important question; “Why on Earth would I ever need to return a value from a string of data?” This is a good question, and the easy answer is you probably will never need to. The fact remains to some that returning a value degrades system performance- but is it enough to worry over?
By looping a large block of text multiple times, we can measure how long the two language constructs take to print out data. In an exclusive LearnPHPOnline.com test, we found that through an extended amount of iterations through a loop, Echo did indeed turn out to be the winner in speed! Whereas it took around 550ms for Print, the complete iteration took around 450ms for Echo. (For those without a calculator or a quick noggin, that’s almost 20% difference.)
Percent Of A Full Second (1,000ms)
Of course results depend on certain conditions, and the fact that we had to iterate the blocks of texts to an unimaginable amount of times to see a result shows that the difference really is marginal. It’s actually mentioned through a supporting page on the PHP.net homepage that developers should pick what suits them best- performance isn’t a real issue.
As a last note on speed, it’s recommended that developers add strings together via parameters- not through concatenation or multiple Echo calls. Instead of using new Echo commands to help organize code, separate them with commas (Make certain you aren’t using concatenation- this actually slows the process)! Calling the Echo or Print command multiple times will also degrade the performance of the script, although marginally, as seen below:
A Special Note On Concatenation Vs Parameters
PHP Echo Vs Print: The Conclusion
So what do we use? Echo of course! But not because of speed, and certainly not because we have anything against pseudo-functions that are disguised as language constructs. So why do most PHP developers go for echo, when the benefits are very marginal?
Easy! It sounds cool! Not to mention the fact that the word Echo has one less letter in it that Print- and that’s saving our left pointing finger from having to press the “T” key each time we want to use the language construct in question.
It’s human nature to be lazy (Or have a certain appreciation for cool-sounding words), and that’s exactly the reason why you’ll see the majority of PHP developers use Echo over Print. The speed benefit is just icing on the cake.
-By Parthiv Patel
-By Parthiv Patel