Page 1 of 2
using echo, which way do you like?
Posted: Thu Jul 28, 2005 11:07 am
by php12342005
using echo: start with single quote or double one?
for a simple string, both are the same.
but in complex case, do you like ' or " as whole quote for echo?
i.e. echo '...' or echo "..."?
advantages and drawbacks?
thanks
Posted: Thu Jul 28, 2005 11:11 am
by nielsene
I tend to only use double quotes, and no concatenation. (So "whole quote" as you're calling it.)
Of course I don't tend to use echo/print too much. Basically just for debugging. I'm a firm beleiver in not letting functions echo -- function that "want" to output, should return strings that the caller can choose to echo or further manipulate. Even at the top level of a web-script, I'll assemble the contents of the page into a string and then echo $page; at the very end. (Basically manual output buffering)
For more complex strings I switch to heredocs.
Posted: Thu Jul 28, 2005 11:28 am
by josh
nielsene wrote:I'll assemble the contents of the page into a string and then echo $page; at the very end. (Basically manual output buffering)
That is the most annoying thing I've ever encountered in some one else's script, don't ask me why I just have issues
I usually use single quotes when outputting basic strings like:
but double quotes for more complex strings
Code: Select all
echo "The value of \$var is $var";
Or sometimes I'll do this
Code: Select all
echo 'The value of $var is '.$var;
Posted: Thu Jul 28, 2005 11:29 am
by onion2k
Always: echo "Hello .. \$var = ".$var." .. yay!";
Posted: Thu Jul 28, 2005 11:33 am
by nielsene
jshpro2 wrote:nielsene wrote:I'll assemble the contents of the page into a string and then echo $page; at the very end. (Basically manual output buffering)
That is the most annoying thing I've ever encountered in some one else's script, don't ask me why I just have issues
You said not to ask, but I have to. What's annyoing about it to you?
I like it for a few reasons:
1. It keeps the code easily amenable to "Extract Method" refactorings.
2. When I do add echo/print/print_r debugging statements, all debugging appears at the top of the page, where it doesn't interfere with layout, etc (aside from makeing a non-validating page)
3. Protection during rapid development against the dreaded "headers already sent"
Posted: Thu Jul 28, 2005 11:37 am
by John Cartwright
Code: Select all
echo 'I love single quotes, don\'t'.$you.'?';

Posted: Thu Jul 28, 2005 11:39 am
by John Cartwright
nielsene wrote:
You said not to ask, but I have to. What's annyoing about it to you?
I like it for a few reasons:
1. It keeps the code easily amenable to "Extract Method" refactorings.
2. When I do add echo/print/print_r debugging statements, all debugging appears at the top of the page, where it doesn't interfere with layout, etc (aside from makeing a non-validating page)
3. Protection during rapid development against the dreaded "headers already sent"
How I do it myself, I completely agree.
Posted: Thu Jul 28, 2005 11:44 am
by josh
I don't have a specific reason, it just bugs me, as does
Curlies on new lines.
I find it's not that hard for me to code in a way where I don't output before I need to send a header, plus if my code is using exits()'s it becomes a pain to write exit($page.$errormsg); every time and it would be easier to write exit($errormsg);
nielsene wrote:
2. When I do add echo/print/print_r debugging statements, all debugging appears at the top of the page, where it doesn't interfere with layout, etc (aside from makeing a non-validating page)
What's wrong with it interfering with the layout if it's just debugging?
Let's not get into a 'holy war' over preference, it's not like it changes the functionality of the code.
Posted: Thu Jul 28, 2005 11:53 am
by nielsene
jshpro2 wrote:I don't have a specific reason, it just bugs me, as does
Curlies on new lines.
You'd *hate* my old code. Both the echo $page and the bad curlies.... but I am switching the curlies to the "condensed" style now... I used condensed style in Java and C but was trained to use separate lines initially in PHP....
I find it's not that hard for me to code in a way where I don't output before I need to send a header, plus if my code is using exits()'s it becomes a pain to write exit($page.$errormsg); every time and it would be easier to write exit($errormsg);
Fair enough, good reason for that style of coding.
nielsene wrote:
2. When I do add echo/print/print_r debugging statements, all debugging appears at the top of the page, where it doesn't interfere with layout, etc (aside from makeing a non-validating page)
What's wrong with it interfering with the layout if it's just debugging?
Let's not get into a 'holy war' over preference, it's not like it changes the functionality of the code.
I found that the layout issues would distract
me from the bug, purely preference as you say.
Posted: Thu Jul 28, 2005 12:15 pm
by josh
Interesting discussion,
To add to it, I remember reading somewhere that there is a slight difference of execution time between:
Code: Select all
ob_start();
for ($x=0; $x<=999999; $x++) {
echo ("hello world");
}
ob_end_clean();
// Output execution time
And
Code: Select all
ob_start();
for ($x=0; $x<=999999; $x++) {
echo ('hello world');
}
ob_end_clean();
// Output execution time
The difference was small but it was there

the double quotes took longer of course, the page also had other interesting benchmarks for PHP related stuff, I think there was a small difference between print and echo in terms of benchmark as well.
In a perfect world PHP would analyze the entire script and see the loop could be omitted, that would be awesome, but would also prevent people from benchmarking the echo function

Posted: Thu Jul 28, 2005 12:29 pm
by nielsene
Yup, there is a very, very minor difference in execution of double and single quoted strings due to the need for increased parsing of the former. However on most "real" sites, the difference is lost in the various "big costs" -- database connections/queries/db-responsiveness (especially if over a netwoerk). Therefore it isn't an optimization that's likely to affect anything. I think I saw another study that showed the difference was roughly comprable to ~1 extra line in a php file being parsed (ie a comment in an include). I dont see people screaming for "no-comments in source code because of parsing costs" so I wouldn't worry about the difference between ' and ".
Posted: Thu Jul 28, 2005 12:48 pm
by josh
nielsene wrote:Yup, there is a very, very minor difference in execution of double and single quoted strings due to the need for increased parsing of the former. However on most "real" sites, the difference is lost in the various "big costs" -- database connections/queries/db-responsiveness (especially if over a netwoerk). Therefore it isn't an optimization that's likely to affect anything. I think I saw another study that showed the difference was roughly comprable to ~1 extra line in a php file being parsed (ie a comment in an include). I dont see people screaming for "no-comments in source code because of parsing costs" so I wouldn't worry about the difference between ' and ".
Yeah I think the site was some php coders that had competitions about who could get their script to do xyz in the fastest amount of time or something, and they were doing anything to increase the speed, but I agree the difference is so small it's not even going to affect anything, I just posted it to add to the discussion.
I know youre supposed to avoid any repetive calls to functions where it's not needed, I heard this can make a
huge difference when dealing with some what large arrays
Code: Select all
$count=count($array);
for ($x=0; $x<=$count; $x++) {
not
Code: Select all
for ($x=0; $x<=count($array); $x++) {
Posted: Thu Jul 28, 2005 12:54 pm
by Ambush Commander
Definitely. If you don't believe me, set up a benchmark yourself.
'' for noninterpolation and "" for interpolation. No sense in "bang". 'bang' is much better (to me anyway). It's also nicer against HTML ("<a href=\"\">" versus '<a href="">'), but that's not really a big problem once you get into templating systems.
When I was first learning PHP, I didn't know what ' and " meant, so I'd do stuff like this:
Code: Select all
echo 'This is a variable and it'."'".'s something you can manipulate. Here is an echo'."'".'ed var: '.$variable;
I'd say just don't switch between ' and " in the middle of strings.
Posted: Thu Jul 28, 2005 12:56 pm
by nielsene
jshpro2 wrote:
I know youre supposed to avoid any repetive calls to functions where it's not needed, I heard this can make a
huge difference when dealing with some what large arrays
Code: Select all
$count=count($array);
for ($x=0; $x<=$count; $x++) {
not
Code: Select all
for ($x=0; $x<=count($array); $x++) {
Yup that is a huge effect and I think the count as variable method reads cleaner too. I have some loops over ~8000 element list and it was an interesting benchmark

don't remember the details, but it was on an old Pentium 1... Calling count() every time timed out, calling one and looping finished in a few seconds...
Posted: Thu Jul 28, 2005 12:59 pm
by Ambush Commander
Well, you probably shouldn't be holding such huge arrays in the memory anyway.