Print VS Echo?

Print VS Echo?

Ok, this has been bugging me for a while and I just have to ask. What is the major difference between print and echo? Often times you see them used interchangeably, and some people swear by one over the other. What is the huge difference between the two?

The Fattest
04/24/2009 - 09:29

The main two differences are going to be a marginal speed increase using echo and the fact that print acts like a function.

The reason echo is faster is because it does not return any value, as apposed to print which always returns 1.

The print function acts like a function which means it can be used in complex expressions. Something like:

$mynum < 10 ? print "less than 10" : print "10 or more";

Where as echo can not be used in that way.

You can also check out more over at http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

reply

The Hairiest
04/24/2009 - 09:46

That's pretty informative. Thanks a bunch.

reply

monkee
08/03/2009 - 03:25

Benchmarking code to view the comparison can be found at:

http://coffeeandpaste.blogspot.com/2009/07/php-echo-vs-print.html

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.