Click to See Complete Forum and Search --> : Single or Double Quotes?


iamlucky13
06-29-2004, 06:08 PM
I've pondered and searched for months, and never had a decent answer to this question:

When should I use single quotes and when should I use double quotes?

When I started coding, I got in the habit of using single quotes in my HTML and double quotes in my PHP. It didn't take me long to notice that most people seemed to do the opposite, but I've never been able to find a reason why. In fact, at the time it seemed logical to use single for my HTML, so I wouldn't have problems with PHP parsing my HTML output improperly (without using backslashes) and so PHP would parse variables when printing strings.

Anyhow, lemme hear your reasons and I will cherish them forever in my heart. Thanks :p

Daniel T
06-29-2004, 06:25 PM
In my opinion, single quotes should be used only inside double quotes, and in JavaScript(when you want it to still recognise variables inside the quotes). Double quotes should be used for everything else. M$ Smart Quotes should never be used for anything! And when using quotes in HTML, it is best to use & q u o t ; (without the spaces).

PeOfEo
06-29-2004, 08:02 PM
Originally posted by Daniel T
In my opinion, single quotes should be used only inside double quotes, and in JavaScript(when you want it to still recognise variables inside the quotes). Double quotes should be used for everything else. M$ Smart Quotes should never be used for anything! And when using quotes in HTML, it is best to use & q u o t ; (without the spaces). ahh like this? "
You just type " to make it work.

The Cheat
06-29-2004, 08:44 PM
in html use double quotes...

in php double quotes, single quotes, and no quotes are all used and each have their own purpose.

Daniel T
06-29-2004, 08:47 PM
Originally posted by PeOfEo
ahh like this? "
You just type " to make it work. Thank you for that, it should come in handy in the future ;)

iamlucky13
06-30-2004, 10:30 AM
Thanks guys.

Is there a reason why double quotes should be used for HTML or just because that's the way it's always been done? I'm going to go back and change those on the site I'm doing right now anyways, but I'm always curious.

The Cheat, we installed that lightswitch so you could turn the lights on and off. Not so you could throw light switch raves. :)

DaiWelsh
06-30-2004, 11:39 AM
In PHP (and Perl) the main reason (at least for me) for using single quotes rather than double by preference is that the compiler/interpreter does not need to search the string for variable substitutions and therefore there is a slight performance gain.

If you want to substitute variables (or if you want to use escape codes like \n for new line) you have to use double quotes and similarly if you want to output literals that could be interpreted as variables or escape codes you must use single quotes or escape the suspect parts.

HTML is more of a convention afaik, I believe even xhtml and xml allow either quote for string literals as long as theya re used consistently.

Edit: in case my meaning of variable substitiution is not clear, try the following in PHP:

echo("<pre>");
$var = 'value';
echo "1: var has value [$var]\n"
echo "2: $var has value [$var]\n"
echo "3: \$var has value [$var]\n"
echo '4: var has value [$var]\n'
echo '5: var has value ['.$var.']\n'
echo '6: where are all my line breaks? ;)\n'
echo("</pre>");