There are countless books and examples in the world with PHP snippets and there is usually one major difference between the code: Print vs Echo - two commands to display $omething…
True Differences
Most people will tell you that there really isn’t a difference between Print and Echo, but they’re technically wrong.
Print and Echo are Language Constructs which just means they are not functions. They behave similarly, but not the same. The basic difference between the two is speed.
Speed Trap
Echo, while it may seem an antique from the BASIC days, is actually faster than Print. How much faster? An average 5%-20% is the agreed upon answer after running loops of code about 1,000 times and comparing the output times. (Honestly, I’d go with the 5%) Now, thats not a big difference, but it depends on what you’re Printing.
If you have a site with a few pages, it may not make a bit of difference which you use. If you have a huge website that is starting to turn into a portal of some sort, Echo would be your best choice - remember, you gain site speed in small steps. A lot of newer coders forget that something so simple can speed up a larger website. This is the case in some of the forum software I see online. The code, while technically working slows to a crawl when a lot of people use it - using echo would be one way to speed it up. (and in most cases, just recoding it would speed it up too)
Now, honestly, I’m a Print kind of guy. This stems from the fact that when I started coding, there wasn’t much to the argument of Print vs Echo. It was more of a “what you like” scenario. At the same time, I’m looking at making my applications much faster. While I’m looking at all kinds of other stuff including DB optimization, using php products for compiling, etc - better coding practices is one of the things one can do to speed things up.
Why?
Why ask why? Just do it!
Seriously, the reason Echo is technically faster is because when you print something, it returns nothing (Void). When using Print, it returns “1″ (true). Now, after printing things over and over, outputting something will slow you down - most of the time.
Here’s The “Technically” Part
Print can be faster when you’re printing small strings. This is mainly 1-2 characters. It is also faster when using compiled PHP…
Examples
10000 iterations per run
long string (790 chars)
echo print
0.0317 ; 0.0321
0.0310 ; 0.0322
0.0312 ; 0.0331
0.0309 ; 0.0318
0.0315 ; 0.0318
0.0369 ; 0.0322
0.0319 ; 0.0352
0.0317 ; 0.0314
0.0319 ; 0.0326
0.0335 ; 0.0333
0.0336 ; 0.0322
0.0316 ; 0.0316
0.0304 ; 0.0309
0.0307 ; 0.0319
0.0305 ; 0.0366
0.0307 ; 0.0318
0.0316 ; 0.0315
0.0309 ; 0.0311
0.0323 ; 0.0318
0.0309 ; 0.0312
totals
0.6354 ; 0.6463
echo (avg. of 20 runs): 0.031770
print (avg. of 20 runs): 0.032315
short string with 2 vars
echo print
0.0489 ; 0.0482
0.0487 ; 0.0501
0.0491 ; 0.0489
0.0487 ; 0.0497
0.0481 ; 0.0479
0.0503 ; 0.0513
0.0486 ; 0.0488
0.0485 ; 0.0484
0.0494 ; 0.0483
0.0511 ; 0.0502
0.0515 ; 0.0491
0.0489 ; 0.0480
0.0499 ; 0.0504
0.0492 ; 0.0485
0.0481 ; 0.0489
0.0489 ; 0.0477
0.0488 ; 0.0487
0.0484 ; 0.0488
0.0497 ; 0.0505
0.0488 ; 0.0508
totals
0.9836 ; 0.9832
echo (avg. of 20 runs): 0.049180
print (avg. of 20 runs): 0.049160
