What is differece between echo and printf in php

Q

What is differece between echo and printf in php

✍: santosh

A

PHP Echo Vs Print Diagram


";
     echo "Hello World! 
"; // The above outputs the text "Hello World!" on two separate lines. // Notice they are identical in output! print ("Hello World!
"); echo ("Hello World!
"); // The above are just the same, with parenthesis. // Notice both can act like functions, but note they actually aren't.PHP Echo Vs Print Diagram "; echo "Hello World!
"; // The above outputs the text "Hello World!" on two separate lines. // Notice they are identical in output! print ("Hello World!
"); echo ("Hello World!
"); // The above are just the same, with parenthesis. // Notice both can act like functions, but note they actually aren't.

n 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.


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

";
// Concatenation slows down the process because PHP must add strings together

     echo "Hello" , "
"; echo "World" , "
"; // Calling Echo multiple times still isn't as good as using Echo parameters solely echo "Hello" , "World!" , "
"; // Correct! In a large loop, this could save a couple of seconds in the long run! ?>

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.

2011-08-01, 4310👍, 0💬