Click to See Complete Forum and Search --> : " vs '
bike5
02-14-2005, 11:53 PM
I see most people use " " in their coding instead of ' ' when you don't need the "".
Like for $name = 'Bob';
I see most people use $name = "Bob";
Is there a reason or is it just personal preferance?
planetsim
02-15-2005, 12:00 AM
Mostly its just personal preference. You may want to search on this there are some other threads quite similar to this.
Mordecai
02-15-2005, 02:07 AM
" is often slower, but easier to use. An example:
$var = "pie";
$string = "Bob like $var";
This then makes $string "Bob likes pie."
However, this:
$var = "pie";
$string 'Bob likes $var';
Does not equate to "Bob likes pie," but, rather, "Bob likes \$var."
Note that the $ is escaped, and there in lies the major difference.
Usually you will not notice any performance gain over using " and ', unless you put a \ or a $ inside. When it hits the $, it must read every possible letter for variable names, and stop when it reads one that doesn't work. For instance:
$var = "pie";
$string = "Bob likes $vars";
Will not work, since $vars is not defined. It does not bother to backwards check for $var, since that would slow it down even more.
So, while personal taste does come in, I prefer to just use ' in most cases, then concatenate variables:
$var = 'pie';
$string = 'Bob likes ' . $var;
sapoxgn
02-15-2005, 06:30 AM
I m aways coding like this:
echo 'text' . $var . 'more "text" and more' . $vars;
btw.. why do people use the /" i still dont understando it :confused:
jonno946
02-15-2005, 07:26 AM
basically using " is slightly slower because it has to process the variable replacements, whilst the ' does not.
laserlight
02-15-2005, 08:25 AM
why do people use the /" i still dont understando it
a) they want to use variable interpolation, or
b) personal preference, since the difference between the two with no variable interpolation is negligible.
andrewtayloruk
02-15-2005, 12:18 PM
What about:
$content = 'This is some content '.$var.'!';
vs
$content = sprintf("This is some content %s!", $var);
When I'm working chunks of HTML, I've started using sprintf(), it's easier to copy and paste.
Anyone elses opinion?
Andrew
jonno946
02-15-2005, 02:11 PM
Originally posted by andrewtayloruk
What about:
$content = 'This is some content '.$var.'!';
vs
$content = sprintf("This is some content %s!", $var);
When I'm working chunks of HTML, I've started using sprintf(), it's easier to copy and paste.
Anyone elses opinion?
Andrew
i would have thought sprintf() would be even slower as it a function, whilst " is just a language construct.
Jonno
Weedpacket
02-15-2005, 11:47 PM
Originally posted by jonno946
i would have thought sprintf() would be even slower as it a function, whilst " is just a language construct. And the example sprintf() code uses "-delimited strings anyway!
But really, if anyone is that desperate to know which is faster, they only need to write a test script. It would only be a few lines. Personally, I find I have more important things to worry about.
Mordecai
02-16-2005, 01:19 AM
Originally posted by sapoxgn
I m aways coding like this:
echo 'text' . $var . 'more "text" and more' . $vars;
If you want to get to the real heart of it, you'd want to use
echo 'text', $var, 'more "text" and more', $vars;
Since echo takes multiple parameters.
andrewtayloruk
02-16-2005, 11:05 AM
You'd have to be doing something seriously wrong with your code to require the speed boost using echo over a function will give you.
For me, the readability, maintainability and ease of development is much more important than micro speed boosts gained from using echo.
I'll leave it up to properly written queries, logical structures, good use of variables and most importantly server-side caching to make my applications fast.
Andrew
davidjam
02-16-2005, 11:17 AM
All about strings (http://us4.php.net/manual/en/language.types.string.php)
I prefer to use single quotes for keys, so I generally use the double quotes:
echo "Bob likes {$var['key']}s"
mfacer
02-16-2005, 12:52 PM
Originally posted by davidjam
I prefer to use single quotes for keys, so I generally use the double quotes:
echo "Bob likes {$var['key']}s"
I'm the same... I used double quotes for echo'ing then single quotes inside key/vars...
echo "This just seems ". $row['easier'] . "<BR>";
I guess like people have said, it's down to personal preference.... It's a habit that probably can never be broken! Once you're in to " or ' ... you're not gonna change!!
bike5
02-16-2005, 05:59 PM
echo "This just seems ". $row['easier'] . "<BR>";
I do it this way
echo 'This just seems '. $row['easier'] . '<BR>';
Thats what I figured it was more personal pref. :D
Roger Ramjet
02-18-2005, 01:43 AM
There is one very simple case for me:
XHTML standard specifies double quotes so I use single quotes: makes the whole thing far more legible not having to escape the double quotes in the XHTML string. Having to close the string and concatenate my vars also makes the code far more legible in my editor which colour codes very nicely.
By the same token, SQL uses single quotes so I use double quotes for my query strings.
Together these two simple practices mean I make far fewer typos and silly syntax errors so I waste less time on them and can concentrate on the logic of my script.
Weedpacket
02-18-2005, 03:26 AM
Originally posted by Roger Ramjet
XHTML standard specifies double quotes There's that urban myth again. I dare you to quote chapter and verse on it ;)
bubblenut
02-18-2005, 06:10 AM
Oh come guys, I know we're supposed to be geeks and have sad conversations about pointless subjects but for Christ's sake, would you take a look at yourselves?
You're discussing the relative merits of string delimiters!
I'm not sure what's more depressing, the fact that this conversation is taking place or the fact that it seems to be the most popular thread the lounge the moment.
laserlight
02-18-2005, 06:53 AM
There's that urban myth again. I dare you to quote chapter and verse on it
I think the XHTML 1.0 spec even gives examples where single quotes are used, and of course the XML 1.0 recommendation explicitly allows either.
Weedpacket
02-18-2005, 09:11 AM
Originally posted by bubblenut
You're discussing the relative merits of string delimiters! I thought I was discussing the merits of discussing the relative merits of string delimiters.
Roger Ramjet
02-18-2005, 09:27 AM
Originally posted by Weedpacket
There's that urban myth again. I dare you to quote chapter and verse on it ;)
All I can offer is the W3C tutorial that I used to get up to speed after not doing any HTML for 4 years. Or maybe it was a Sitepoint article? Whatever!! All that matters is that one be consistent whichever one chooses.
As I said, the single quote syntax prevents me from embedding vars in my strings which makes for far more readable, MAINTAINABLE code (and excellent colour coding in my editor).
You're discussing the relative merits of string delimiters!
No. We are discussing the relative merits of different ways to STRUCTURE our code: and if you don't know the importance of that then I'm sorry for you.
bubblenut
02-18-2005, 10:20 AM
Originally posted by Roger Ramjet
No. We are discussing the relative merits of different ways to STRUCTURE our code: and if you don't know the importance of that then I'm sorry for you.
I'm all for conversations about code structure but I wouldn't really call the difference between ' and " as a string delimiter a code strucutre issue. Whether to use a decorator, factory or builder is a structure issue. Whether to place the call to the DB right before the output or earlier in the page is a structure issue. I can hear the cry already "No they're design issues!", which is perfectly true but they are also structure issues as they are actually going to affect the structure of my program. Using " or ' isn't going to affect the structure of my program any more than the decision to write my for loops like for($i=0, $c=count($array); $i<$c; $i++) { or for($i=0; $i<count($array); $i++) {. Some processing time issues may arise if I'm dealing with huge arrays (as they may with huge amounts of string data but we're talking huge) but it's not actually going to change the structure of my program.
/me runs and hides under a rock before
davidjam
02-18-2005, 10:42 AM
There are those who say that form is function, those who say form is only aesthetic and those who say that form is "the nature of the thing", but I would say that form is integral to function. If the "shape" of the code causes confusion then function will eventually fail. I think they call this "elegance" in programming...
/me hides under the primal cabbage leaf
Roger Ramjet
02-18-2005, 10:55 AM
Originally posted by davidjam
If the "shape" of the code causes confusion then function will eventually fail. I think they call this "elegance" in programming...
/me hides under the primal cabbage leaf
I call it software engineering. To me it is the difference between a proffessional and a talented amateur.
I know it's a pain writing 'structured code' (which is nothing to do with program structure), but it has to be done. Anyone who has had to maintain code written months or years ago by some 'clever-clogs' who tried to do it all in as few lines as possible and documented nothing knows exactly what I'm talking about. As will those who have worked in large projects outside of web programming.
/me with a primal scream
laserlight
02-18-2005, 12:14 PM
Using " or ' isn't going to affect the structure of my program any more than the decision to write my for loops like for($i=0, $c=count($array); $i<$c; $i++) { or for($i=0; $i<count($array); $i++) {
hmm... but the decision pertaining to the loop does affect your (algorithmic) structure in the sense that you're taking O(n**2) time for something that should take O(n) time, if you use the latter example.
cafrow
02-18-2005, 12:58 PM
Here is another example where I learned when to us ' instead of "
echo ' Visit us at <a href="deletethekernel.com" target="_blank">DTK</a>';
now you had to use ' because the " would have made the HTML conflict witht he PHP
bubblenut
02-18-2005, 02:05 PM
Right, I think I'm being a little misunderstood here. I'm not saying that style and format are not hugely important. What I am trying to get at is that the decision between ' & " needn't be a major issue so long as consistency is present. I personally tend to use double quotes almost everywhere I'm not using heredoc (apart from associative arrays, dunno why) for the sole reason that then I'm using double quotes almost everywhere, ie. consistency. It's not an issue I'm willing to spend any time thinking about because the time saved in increased execution time would be lost in the extra microseconds it would take to make the decision of which to use in which situation. Yes, the processing time saved is that small. I'd far rather just not think about it at all and spend my time optimizing the database structure & indexing, SQL queries, I/O, recursion and all the other things which regularly have a major impact on the speed of my programs.
laserlight: I'm not too big on my Big O notation but I'm not quite sure how you got to that result. I understand that the second method would take a little longer as it has to recalculate the size of the array every iteration but surely this will only have a big impact on the O of the loop if it's empty. Like I say, I don't know big O well at all but I'd love an explanation.
Elizabeth
02-18-2005, 02:27 PM
Originally posted by bubblenut
Like I say, I don't know big O well at all but I'd love an explanation.
<---not going there.
AstroTeg
02-18-2005, 03:07 PM
Surprised nobody has mentioned this yet: A " (double quote) requires simultaneously pressing 2 keys while a ' (single quote) requires just one. In theory, the single quote should be easier to type.
/me: Captain Obvious
davidjam
02-18-2005, 03:34 PM
...and considering the fact that holding down the shift key whilst pressing "/' is rather more unhealthy for carpal tunnel sufferers, (which is why I use sticky keys on the mac), but on the other hand concatenating variables because single quotes doesn't parse them requires 4 extra keystroaks: i.e. 'Hello '.$name.'!' vs. "Hello $name!" or 2 extra if you use "Hello {$name}!". I wonder if these work: 'hello $name['key']!' or does it have to be 'hello {$name['key']}!' ?
// gently stretching the thread :rolleyes:
bubblenut
02-18-2005, 03:45 PM
AAARRRRGGGHHHH!!!!
It's like a recurring nightmare (or maybe even /. :D), I swear this thread is almost exactly the same as the last one on the subject!
Bubblenut ties himslef up in a straight jacket and runs off to Maudsley :p
laserlight
02-18-2005, 09:21 PM
What I am trying to get at is that the decision between ' & " needn't be a major issue so long as consistency is present.
I agree, since there are other things that usually take up much more time than output to the user.
Then again, there are a group of optimisation freaks online with a need for speed... :D
I understand that the second method would take a little longer as it has to recalculate the size of the array every iteration but surely this will only have a big impact on the O of the loop if it's empty.
hmm... my idea was that since on every iteration one looped through the whole array again, then the total number of iterations is at least n^2.
Dont worry, I'm not too big on that notation too :)
Weedpacket
02-18-2005, 10:48 PM
Originally posted by AstroTeg
Surprised nobody has mentioned this yet: A " (double quote) requires simultaneously pressing 2 keys while a ' (single quote) requires just one. In theory, the single quote should be easier to type. Well, that shouldn't matter if the next or previous character needs the shift key as well, should it?
I'm going, now. This is getting too silly even for me. I'll come back later if I can come back with a suitably dodgy innuendo about big O - something about how wide it has to open before it can take the whole thing in.
bike5
02-19-2005, 08:48 PM
Mostly its just personal preference.
The first reply I got, makes the most sense out of all the rest and is simple. Works for me. :D
But the rest of the replies were interesting and relatively amusing...;)
Weedpacket
02-19-2005, 08:54 PM
Originally posted by bike5
But the rest of the replies were interesting and relatively amusing...;) If they weren't then it would mean that Elizabeth's not doing her job. And that is very unlikely....
Elizabeth
02-20-2005, 07:49 PM
I try my best to bring the EL down to my level whenever possible--we do have our standards here.
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.