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?
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?
Mostly its just personal preference. You may want to search on this there are some other threads quite similar to this.
" 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;
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
basically using " is slightly slower because it has to process the variable replacements, whilst the ' does not.
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.
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
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
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.
Originally posted by sapoxgn
I m aways coding like this:
echo 'text' . $var . 'more "text" and more' . $vars;
[/B]
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.
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
I prefer to use single quotes for keys, so I generally use the double quotes:
echo "Bob likes {$var['key']}s"
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"
[/B]
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!!
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.
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.
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
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.
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.
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.
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.