PHP single vs. double quotes what’s the diff?

Using single vs. double quotes when handling strings in PHP (and code in general). This article is a re-hash of experimentation done about 6 years ago with PERL. It was very clear that unless you have a VERY compelling reason to use double-quotes with strings.. you shouldn’t do it.

Some people will ask.. “Why, what’s the diff”? Well, simply put.. double quoted string are more work for interpreted code languages such as PERL and PHP (and possibly others too, but I’ve never tested them). Compiles languages should not be subject to such unfortunate circumstance.

The Short of it

Using double quotes vs. single quotes in string copies or setting will cost you and extra processing time (proofs follow).

However, when it comes to variable substitutions, that’s where you’ll see more of the speed benefit, when not forcing PHP to interpret the string looking for variables to substitute.

Although, one interested finding after multiple test runs was that bounding the variable with brackets does not offer a consistent benefit, and often it’s a slight loss of speed.

Here is the raw comparison of the following string copies (heavily iterated):

The Raw Data

$x = “THIS IS A STRING” 1.336
$x = ‘THIS IS A STRING’ 1.187
$x = “THIS IS A STRING $i” 3.004
$x = “THIS IS A STRING ${i}” 3.015
$x = “THIS IS A ${i} STRING” 3.448
$x = ‘THIS IS A STRING’.$i 2.647
$x = ‘THIS IS A ‘.$i.’ STRING’ 3.488

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.