Killersites.com Homepage Welcome Guest   |   Register  |  Login
Login Name Password
  Search  
  Index  | Recent Threads  | Unanswered Threads  | Who's Online  | User List  | Help


Quick Go »

No member browsing this thread
Thread Status: Active
Total posts in this thread: 1
[ Jump to Last Post ]
Post new Thread
Author
Previous Thread This topic has been viewed 879 times and has 0 replies Next Thread
Male ewwatson
Advanced Member
Member's Avatar

United States
Joined: Dec 19, 2007
Post Count: 1285
Status: Offline
Reply to this Post  Reply with Quote 
print vs echo - some good points made for each

These are not my words, but I thought I would place them here rather than just providing links. I know this discussion has probably been done to death elsewhere, but I just found each different perspective interesting. Bottom line it doesn't really matter, but I thought they each had good points to make.

Pro print -
You will notice as soon as you start programming in PHP that you have two options when it comes to displaying text on the screen: Echo or Print. Both examples shown below will display exactly the same text on the screen in exactly the same way. So, which one should you use?

<?php
print "Hello World!";
?>

<?php
echo "Hello World!";
?>

There is one small difference between print and echo. With print you can return a true or false value which means you can use it as part of a more complex expression. Because the print function gives you this extra functionality which may be of use, I would suggest simply using print all the time.

Of course, because echo does not return a value, it can be argued that echo is faster than print. This is true, though the difference in execution time is so small that in practice it is negligible. In the past, it may have been possible to speed up a large PHP application by several milliseconds by converting all prints to echos, but with the speed of today's computers I doubt there would be any useful performance improvement. If you need to improve the performance of your program, optimizing your algorithms will be way more beneficial than switching to echo.

At the end of the day, it's really up to the preference of the programmer when it comes to choosing between Echo and Print. As I've mentioned, I've gone with only using Print because I feel that my code is more easy to understand and therefore of higher quality this way than if I began mixing both Echo and Print. If I just used echo, I may still need to use Print in some cases should I need to return a value, therefore I would inevitably find myself mixing both functions and confusing any new PHP programmers I share my code with! Also, the print command is more common in other programming languages and is more intuitive which are some other reasons I have decide to stick with just using Print.
http://www.webdevnotes.com/echo-vs-print-which-should-be-used/

Pro echo -
After talking to a few web developers and reading through a lot of other people's code, I have come to the conclusion that most people use the "echo" command to output text. What is interesting, is that many tutorials and beginner resources in fact teach the "print" command to accomplish task.

On the surface, echo and print seems to accomplish the same thing, but they have a couple of subtle differences.

Print returns a boolean
When you call the print function, it returns either true or false based on whether or not it executed properly. Therefore, you can do something like this with print but not with echo:

if ( !print("hello!") ) {
die("print failed");
}

Echo is Faster
Since echo does not return a boolean value, it is naturally faster as empirical tests have demonstrated. However, the difference is so small that when it comes to actual use, it is pretty much negligible.

So Which to Use?
Honestly, it doesn't matter. But keeping in the spirit of efficiency, echo should be used all the time unless you require the boolean return value of print. I'm having difficulty coming up with a situation where a boolean return value would be useful in an output function. If anyone has a good example, I would love to hear it!

P.S. Echo is also easier to type as there are less letters and less transitions between hands.
http://www.jonlee.ca/php-tidbit-echo-vs-print/
----------------------------------------
Eric :~)
Knowledge is Power
----------------------------------------
[Edit 3 times, last edit by ewwatson at Mar 3, 2008 7:46:42 PM]
[Mar 3, 2008 7:39:14 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
[ Jump to Last Post ]
Show Printable Version of Thread  Post new Thread