using echo, which way do you like?
Moderator: General Moderators
-
php12342005
- Forum Commoner
- Posts: 79
- Joined: Mon Mar 21, 2005 3:35 am
using echo, which way do you like?
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
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
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.
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.
That is the most annoying thing I've ever encountered in some one else's script, don't ask me why I just have issuesnielsene wrote:I'll assemble the contents of the page into a string and then echo $page; at the very end. (Basically manual output buffering)
I usually use single quotes when outputting basic strings like:
Code: Select all
echo 'hello world';Code: Select all
echo "The value of \$var is $var";Code: Select all
echo 'The value of $var is '.$var;You said not to ask, but I have to. What's annyoing about it to you?jshpro2 wrote:That is the most annoying thing I've ever encountered in some one else's script, don't ask me why I just have issuesnielsene wrote:I'll assemble the contents of the page into a string and then echo $page; at the very end. (Basically manual output buffering)![]()
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"
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
echo 'I love single quotes, don\'t'.$you.'?';- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
How I do it myself, I completely agree.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"
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);
Let's not get into a 'holy war' over preference, it's not like it changes the functionality of the code.
Code: Select all
if ($var)
{
dostuff();
}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);
What's wrong with it interfering with the layout if it's just debugging?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)
Let's not get into a 'holy war' over preference, it's not like it changes the functionality of the code.
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....jshpro2 wrote:I don't have a specific reason, it just bugs me, as doesCurlies on new lines.Code: Select all
if ($var) { dostuff(); }
Fair enough, good reason for that style of coding.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);
I found that the layout issues would distract me from the bug, purely preference as you say.What's wrong with it interfering with the layout if it's just debugging?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)
Let's not get into a 'holy war' over preference, it's not like it changes the functionality of the code.
Interesting discussion,
To add to it, I remember reading somewhere that there is a slight difference of execution time between:
And
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
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 timeCode: Select all
ob_start();
for ($x=0; $x<=999999; $x++) {
echo ('hello world');
}
ob_end_clean();
// Output execution timeIn 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
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.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 ".
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++) {Code: Select all
for ($x=0; $x<=count($array); $x++) {- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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:
I'd say just don't switch between ' and " in the middle of strings.
'' 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;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 arraysnotCode: Select all
$count=count($array); for ($x=0; $x<=$count; $x++) {
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
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US