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

asked Jun 3, 2014 at 15:05
1

1 Answer 1

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.

answered Jun 6, 2014 at 11:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.