Hi! I was wondering, what could I use best for printing text in PHP: Echo or Print? And why should it be Echo or Print? I heard that servers can process Print faster, is this true? Thanks for sharing your opinions and knowledge! Bram Wenting
echo is faster cause it doesn't leave a return value like print. But this is really nothing you should worry about.. use whatever you prefer.
Afaik echo is marginally faster as it does not return a value (true/false on success/failure) but there's really not much in it. Also when outputting multiple variables and strings you don't need to concate into a single argument but can pass each variable as a separate parameter: echo $var, $var2, $var3; print $var.$var2.$var3
If I have to print a value(s) to a page i use print because I can usually format the line or variables that need to be printed. ex. print '<h3><center><b>' . $this->errmsg . '</h3></center></b><p><br>'; This way I can use html or php formatting to give a cleaner effect to the screen for viewers that go to my site.
I would have to agree DXL plus I am use to using print from writing C++ code all the time. I only used echo when I first started programming.
There is a some difference between the two, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do: $ret = print "Hello World"; and $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty. If the grammar is: echo expression [, expression[, expression] ... ] then echo ( expression, expression ) is not valid. ( expression ) reduces to just an expression so this would be valid: echo ("howdy"),("partner"); but you would simply write this as: echo "howdy","partner"; if you wanted to use two expressions. Putting the brackets in there serves no purpose since there is no operator precedence issue with a single expression like that. Feel free to use any according to your needs. *NIX geeks will prefer "echo" though.