Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
1  
There 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 Jun 4 at 4:19
add comment

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.

http://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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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