0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.