开发者

Why some famous programs are always using print [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

I've read somewhere, do not remember where now, that echo is a more efficient way of outputtin开发者_Go百科g data then print.

Why do many famous software packages and frameworks, like WordPress and Drupal, like print so much? Is there any special reason behind that, or just a habit?


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.

You can find some more reference here :

http://www.learnphponline.com/php-basics/php-echo-vs-print

Not entirely complete, though. Print can be used as part of complex constructs, such as

($b) ? print “True” : print “False”;

whereas Echo cannot. Also, if you want to use error output (@print”Test”;) you cannot use echo. Otherwise – good info.


There is nothing in the PHP docs to support this claim. However, a key difference between the two is that echo does not return a value and print does. So one could make an argument that echo is therefore more efficient.

Checkout the PHP Benchmark for more information about echo vs print and other interesting comparisons.

In the end, such things boil down to personal convention. Whatever efficiency gained from using echo over print is more than likely trivial relative to other areas of the code.


In performance tests I have seen no difference in speed between print and echo in PHP so they are interchangeable. Really it's personal preference. In the wild I've seen strictly PHP programmers stick to echo and multi-lingual programmers (if that's a phrase) use either.


Because you read those things on blogs who like to talk about micro-optimizations. You should avoid premature optimization at all costs because it is not worth the effort => You should google famous quote by http://en.wikipedia.org/wiki/Tony_Hoare. You should only tackle your low hanging fruit when you have performance problems and stop wasting your time reading blogs like that. The creator of PHP gave a couple of presentations how to do that => http://www.archive.org/details/simple_is_hard

By now it is a pretty old video, but still very good if you ask me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜