views:

5062

answers:

7

I am a big time user of using double quotes in PHP so that I can parse variables without concatenating strings. As a result, when I am generating HTML I often use single quotes for setting tag fields. Example:

$html = "<input type='text' name='address' value='$address'>" ;

Now this is far more readable to me then either

$html = "<input type=\"text\" name=\"address\" value=\"$address\">" ;

or

$html = '<input type="text" name="address" values="' . $address . '">' ;

from brief searches I have heard people saying that single quotes for HTML fields is not recognized by EVERY browser. Thus I am wondering what browsers would have problems recognizing single quote HTML?

+14  A: 

This is similar to When did single quotes in HTML become so popular?. Single quotes around attributes in HTML are and always have been permitted by the specification. I don't think any browsers wouldn't understand them.

Greg Hewgill
+2  A: 

I also tend to use single quotes in HTML and I have never experienced a problem.

Unkwntech
+3  A: 

I have heard people saying that single quotes for HTML fields is not recognized by EVERY browser

That person is wrong.

FlySwat
A: 

Single Quotes are fine for HTML, but they don't make valid XHTML, which might be problematic if anybody was using a browser which supported only XHTML, but not HTML. I don't believe any such browsers exist, though there are probably some User-Agents out there that do require strict XHTML.

Ultimately it doesn't matter (or rather, there are things which matter a lot more) in web programming. You may get slightly faster rendering in some browser engines depending on how they're implemented, but we're talking a difference of a few milliseconds, nothing "real".

Adam N
the worse part is people that write HTML pages but mark them as XHTML because it's 'better'. fortunately this fad seems to be going down.
Javier
I do not believe this statement about XHTML is true. Both " and ' are acceptable in XML, and the W3C validator accepts XHTML documents with single-quoted attributes. Perhaps this may be a confusion with XHTML eliminating unquoted attributes which are legal in HTML?
Doug McClean
Unless you serve your page as text/xhtml and not text/html browsers will render it as HTML, so HTML rules will apply. Regrdless, one on the w3C principles is NOT TO BREAk THE WEB. Because it works now, it will likely work tomorrow.
Diodeus
XHTML requires pages to be well-formed XML, and XML allows either double or single quotes around attributes.
Ned Batchelder
Actually, to be treated as XHTML you need application/xhtml, not text/xhtml. I made the same mistake with my web site and was (unpleasantly) surprised that it wasn't text/...
Software Monkey
+4  A: 

Don't believe everything you see on Internet...
Funnily, I just answered something similar to somebody declaring single quotes are not valid in XHTML...

Mmm, I look above while typing, and see that Adam N propagates the same belief. If he can back up his affirmation, I retract what I wrote... AFAIK, XML is agnostic and accepts both kinds of quote. I even tried and validated without problem an XHTML page with only single quotes.

PhiLho
+8  A: 

As noted by PhiLho, although there is a widely spread belief that single quotes are not allowed for attribute values, that belief is wrong.

The XML standard permits both single and double quotes around attribute values.

The XHTML standard doesn't say anything to change this, but a related section which states that attribute values must be quoted uses double quotes in the example, which has probably lead to this confusion. This example is merely pointing out that attribute values in XHTML must meet the minimum standard for attribute values in XML, which means they must be quoted (as opposed to plain HTML which doesn't care), but does not restrict you to either single or double quotes.

Of course, it's always possible that you'll encounter a parser which isn't standards-compliant, but when that happens all bets are off anyway. So it's best to just stick to what the specification says. That's why we have specifications, after all.

Adam Bellaire
A: 

I have experienced problems with single quotes in FF3.5.3. I know, hard to believe, but it is true. Specifically, I had a problem when writing "<a href='mailto:[email protected]'>My Name</a>".

Anyone else ever experience anything like that?

Patrick
Okasillydillyokiok... I'm an idiot. Sorry, I was debugging my code and introduced my own error because I escaped my single-quoted data for database entry before I wrote it out to the page. Sorry for the unvetted comment
Patrick