-3

Possible Duplicate:
When did single quotes in HTML become so popular?

Should I use ' or " when coding, for example width='100px' or width="100px". Is it only a matter of taste, or does it matter to the browsers?

The reason why I ask, I have always used "" for everything, so when I code with PHP, I have to escape like this:

echo "<table width=\"100px\>"";

But I've found out that I will probably save 2 minutes per day if I do this:

echo "<table width='100px'>"

Fewer key strokes. Of course I could also do this:

echo '<table width="100px">'

What should the HTML look like; 'option1' or "option2"?

Community
  • 1
  • 1
lawrence
  • 35
  • 4
  • 6
    You had me up until that last sentence. EDIT: leaving the comment even though it doesn't apply now. Check the edits of the question. uggh! – David Hoerster Aug 19 '10 at 03:16
  • 1
    I removed the offending last sentence. – Timwi Aug 19 '10 at 03:18
  • now I need to know what the last sentence was. – Kenneth J Aug 19 '10 at 03:21
  • Consider using here-docs for anything more than a line. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc – Schwern Aug 19 '10 at 03:22
  • 3
    @KennethJ It was "now i have too poop some diarrhea", which may, or may not, have relevance to one's HTML syntactic style preferences. – Chris Fulstow Aug 19 '10 at 03:23
  • 1
    @Kenneth J it was a poop joke. or something. You're not missing anything you didn't hear in junior high. – Shaggy Frog Aug 19 '10 at 03:23
  • It's surprising how much different the poster sounds now as compared to his original post. – AHungerArtist Aug 19 '10 at 03:24
  • 1
    I don’t agree that it’s an exact duplicate of that. – Timwi Aug 19 '10 at 03:28
  • @Schwern: Shudder! Heredoc syntax is the most unreadable mess in PHP and Perl code. Use multi-line literal strings instead, which you can indent properly with the rest of the code (the extra spaces will be in the HTML, but obviously that doesn’t matter to the HTML). – Timwi Aug 19 '10 at 03:29
  • 1
    Lawrence, don't roll back the edit please. I don't care how much diarrhea you have pooped, you can leave it off your question. –  Aug 19 '10 at 13:46

5 Answers5

6

Yes, it's a matter of taste. It makes no difference in HTML. Quoting the W3C on SGML and HMTL:

By default, SGML requires that all attribute values 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.

...

In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them.

However note that the width attribute is deprecated, even though it is still supported in all major browsers. Actually the width attribute is not deprecated when used in <table> in HTML 4.01. Only when used in <hr>, <pre>, <td>, <th> (Source 1, 2, 3, 4).

Community
  • 1
  • 1
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
1

HTML5 supports attributes specified in any of these four different ways:

  • Empty attribute syntax: <input disabled>
  • Unquoted attribute value syntax: <input value=yes>
  • Single-quoted attribute value syntax: <input type='checkbox'>
  • Double-quoted attribute value syntax: <input name="be evil">
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
0

For me it is better to use ' than "". PHP will parse the string wrap with "" and using ' is faster because PHP will treat it just a string and no more parsing.

Manie
  • 2,028
  • 2
  • 16
  • 18
0

I agree with Daniel - it's a matter of taste.

Personally, I use double quotes when assigning values to attributes (e.g. width="100%"). Also, if you deal with JSON, strictly speaking, the names and values should be surrounded with double quotes ("").

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
-2

echo '<table width="100px">'

Because other HTML tags are probably with double quotes, keep it clean, you don't need to escape quotes this way.

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66