Double quotes or single quotes inside html tag, which one is the better?

If you google “Double quotes or single quotes inside html tag, which is better”, you might hear someone is talking about using double quotes in html won’t pass XHMTL validation. I think it is nonsense.

Both of these characters are valid for XHTML, i.e. both

<tag att="value">


<tag att='value' >

are valid. However, I have a good reason to encourage you to stick to double quotes.

When value is taken from user input, it is a must to convert all applicable characters to HTML entities by htmlentities(), and htmlentities() default flag is ENT_COMPAT which convert double quotes but leave single quotes alone. That is to say,

If I use

<tag att="(value taken from user input)">

constantly in templates, and if htmlentities() converts all double quotes in user input, whatever user input is, it will not break html output.

I used to use double quotes and single quotes interchangeably, but I learned from Magento. Magento sticks to using double quotes. Its htmlEscape() bridges to htmlspecialchars() but it does not let you pass in a flag to control how to convert double quotes or single quotes. So, if you use

<tag att='(value taken from user input)'>

, and if you rely on Magento htmlEscape() to convert special charcters for you, when user input has some single quotes, it ruins html output. I am sure you do not want it happens, so stick to using double quotes, always.


芳草苑很久没升级了,今天重新跟上 WordPress 主节奏,更新到 version 3.0.3。

这次更新的主要原因是默认主题 twentyten 很有 html5 的味道;拖了这么久才更新是当初想把当时的 default theme 改为流式布局的任务一直没完成。如今改进 default theme 已无大意义,就彻底放弃此念头。


老版本最后一个 Google 页面预览照

I decide to follow html5 from now

关于 html5 的消息时有耳闻,今天看到的 contenteditable 属性让我兴奋不已,所以我决定开始关注 html5。

看看,有了 contenteditable = “true”,以后接受用户输入有了更灵活的方式。

本文就是 contenteditable。