PHP Tidbit: Single Quotes vs Double Quotes
Posted by Jon Lee in Web Development, tags: double-quotes, echo, PHP, single-quotes, tidbitHere is another little PHP tidbit. I actually learned this through trial and error and found it documented only a short while ago. Single quotes and double quotes: what’s the difference? Both single quotes and double quotes in PHP accomplish the same function, that is, wrapping text. For example:
echo “hi”;
echo ‘hi’;
Both produce the same output (simply the word hi). However, there are situations where one is better than the other.
Outputting HTML
HTML tags use a lot of quotes. It is generally accepted that these quotes should be double quotes. Single quotes do work but isn’t the convention. No quotes also work with single word attributes but doing so would make most web developers cry.
As such, when outputting HTML from PHP, it can be better to use the single quote because then double quotes within are treated as part of the output.
echo ‘<div id=”heading” class=”blue” align=”center”>Some text</div>’;
will be outputted as the following in HTML:
<div id=”heading” class=”blue” align=”center”>Some text</div>
With double quotes, the exact same output would require backslashes to escape double quotes (have PHP recognize them as part of the output and not as part of the PHP syntax). It would look something like this:
echo “<div id=\”heading\” class=\”blue\” align=\”center\”>Some text</div>’;
Definitely doesn’t look as nice nor is it copy & paste friendly if you wish to move that code into a plain HTML document.
Outputting Variables
Take a look at this example:
$name = “Jon”;
$clothing = “hat”;
echo “Hello $name, nice $clothing you’re wearing!”;
echo ‘Hello $name, nice $clothing you’re wearing!’;
The first output line above produces the following output:
Hello Jon, nice hat you’re wearing!
That’s all fine and dandy and it’s exactly what we want. Let’s look at the second line:
Hello $name, nice $clothing you’re wearing!
As you can see, double quotes will recongize variables and substitute them into the string. Since single quotes do not, to do the same thing your code will look something like:
echo ‘Hello ‘ . $name . ‘, nice ‘ . $clothing . ‘ you’re wearing!’;
Simply hideous don’t you think?
Outputting Special Characters
Certain special characters are only recognized inside double quotes. The best examples are probably the new line characters \n and \r. Only inside double quotes are they actually new line characters (useful for formatting HTML outputted from PHP). In single quotes, they are merely a backslash followed by a letter.
Conclusion
Really, there is no strict rule saying when to use one or the other or both; it really depends on the situation. And these are definitely not all the situations where one is preferred over the other, but they are situations that I think are very common in web development. If you have any other scenarios I would love to hear about them in the comments!
Popularity: 5% [?]
Entries (RSS)
Darn. I want everyone to go to Postie con! Have you checked
http://www.hotwire.com/index.jsp?sid=S20&bid=B769
‘ is faster then ” because it is not parsed for possible variables…
If speed matters… Go for ‘
Personally, I prefer
echo ‘Hello ‘ . $name . ‘, nice ‘ . $clothing . ‘ you\’re wearing!’;
to
echo “Hello $name, nice $clothing you’re wearing!”;
Don’t know why, it’s just the way I like to code.