If I'm used to use ' for string literals in PHP, would it be better if I'll stick to it in any situation or can there be exceptions when it would increase readability?
example:
$foo = "bar'baz"; // this may be easier to read
$foo = 'bar\'baz'; // but this would stick to the convention
-
1There seems to be significant differences in single vs. double quotes in PHP. See What is the difference between single-quoted and double-quoted strings in PHP?.metacubed– metacubed2014年06月04日 04:19:42 +00:00Commented Jun 4, 2014 at 4:19
1 Answer 1
There is a long held (and mostly untrue) PHP superstition that double-quoted (DQ) strings are "slower" than single-quoted (SQ) strings, due to the fact that DQ strings can do variable interpolation and SQ strings cannot.
Many coding convention guides, such as the Media Wiki coding convention guide (http://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP) say things such as
For simple string literals, single quotes are slightly faster for PHP to parse than double quotes.
This has been generally disproven, with a small number of exceptions. See http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html for a more in-depth explanation.
The only exception to this rule is if your DQ string contains a $. If it does, the string will be parsed twice, and interpolation performed. So for short strings such as the ones in your example, there is no difference in speed, and in my opinion the gain in readability is an advantage.
https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php (linked above by metacubed) contains much more information on the difference between SQ and DQ strings.