Since both are acceptable by HTML as well as languages like ASP.NET and PHP when using attributes or strings, why is it that some people use single quotes and double quotes interchangeably?
It is my understanding that it is syntactically correct to use double quotes where possible, single when you need to embed a double quote for inline logic开发者_Go百科.
Is there something I am missing?
For examples:
HTML
<a href='http://google.com'>Google</a>
PHP
<? echo 'Hello World!'; ?>
ASP.NET
<form id='myForm' runat='server'></form>
Technically, in PHP single quotes are faster since you don't need to parse the content within.
edit: So double quotes are automatically converted to single quotes, but if you have variable substitution going on within your double quoted string, that's when you take a performance hit:
http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes
Either ways, to answer OP's question while the jury is out on this, play it safe (TM) and use single quotes :)
In HTML, I don't think the "why" can be answered in anything but the obvious case: single quoted strings are more convenient when the string contains double quotes, and vice-versa.
In PHP, single quoted strings are more convenient when you don't want any special interpolation or escape characters.
My personal preference is always use double quotes in HTML, and to always use single quotes in PHP unless I need interpolation. Thus, I consider the single quoted strings to be "constants" of sorts in PHP, while the double quoted string implies something else is going on.
<opinion>But why do some people whimsically choose between the two? Probably because they are undisciplined and subsequently not very good programmers.</opinion>
From W3C: http://www.w3.org/TR/html4/intro/sgmltut.html
All attribute values [should] be delimited using either double quotation
marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39).
Single quote marks can be included within the attribute value when the
value is delimited by double quote marks, and vice versa.
Strings in PHP follow the same principle - interchangeable single/double quotes.
I would say that for most people, single and double quotes are treating and used interchangeably without a real understanding of the difference.
Both are used to create/delineate strings.
'Hello'
"Hello"
Both are strings and are treated the same when used in this circumstance.
The difference in in processing. Technically, single quotes strings are not processed when created and stored in to memory. They are taken as is and made into strings.
Double quoted strings are processed when created and stored into memory. That is why you can put a variable into a double quoted string and it's value will be put in, but in a single quoted string the literal variable is put in. For most things, there is not a real difference if you sing a single or double quote except when creating strings with variables, function calls, etc and for saving some milliseconds in processing.
Basically, the choice is yours. But for readability & maintainability, pick one form & stick with it.
I find the use of single quotes advantageous when I'm embedding html into strings, mainly when dealing with templating. Here is an example:
public string EmailTemplate =
@"<div style='color:red'>HEY {0}! BUY MORE STUFF</div>"
// later in code
instanceOfStringBuilder.AppendFormat(EmailTemplate, firstNameVariable);
I don't have any hierarchy in my mind for whether single or double quotes are "better." It is purely a matter of being consistent and having something that programmatically works.
I agree with @stillstanding on the issue of interchangeability within HTML. However, in PHP I use double quotes in instances where I need a variable within a string parsed. Consider this:
<?php
$id = 123;
echo "Your id is $id<br />";
echo 'Your id is $id';
?>
This will output
Your id is 123
Your id is $id
精彩评论