using quotes and double quotes

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
meba's picture

This is about PHP common development mainly, some advices to improve strings parsing performance.

Do you know the difference of using:

<?php
$var
= "My nice variable";
$var2 = 'My nice variable';
?>

?

The main difference is: PHP doesn't check string content for included variables when using single quotes. See these examples:

<?php
$var
= "You have: $coins coins";
$var = 'You have:'.$coins.' coins';
?>

In first case, PHP evals "string" for included variables and insert value of $coins. In second example, strings are joined together with value of $coins. Second is FASTER.

This is for all Drupal core developers and module developers: Use single quotes wherever possible. Drupal core is very good and uses single quotes most of the times, but the modules...just slowing things down...

(Please note that there shouldn't be any big difference of using "You have".$coins." coins" and 'You have'.$coins.' coins' and in recent versions of PHP, parsing code has been rewritten to improve performance, but practice is always a 'good practices')

Comments

re: using quotes and double quotes

RichardLynch's picture

In the unlikely event anybody finds the above and believes it, please don't.

There is NO SIGNIFICANT ADVANTAGE to 'apostrophes' versus 'quotes' in any meaningful way in PHP.

First, the engine has to examine each character in either version, looking for \ character, because \' and \ are significant inside 'apostrophes'.

So it cannot simple "skip" to the next ' character.

Second, any serious profiling of code has always proven that the difference is on the order of microseconds for millions of strings.

That's not microseconds per string -- Microseconds for ALL of the millions of strings.

Use Xdebug or similar to find your real bottle-necks instead of Voodoo Optimization.

Richard the original post (by

bserem's picture

Richard the original post (by meba) dates back to 2006. Things might have been different back then, very different.

There are some benchmarks available here: http://www.phpbench.com/
There is no big difference between " and ' and the authors conclusion is:

In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!

Bill Seremetis
http://srm.gr - working with Drupal in Greece

Agreed that the performance

Garrett Albright's picture

Agreed that the performance difference is slight. However, out of habit, I always use single quotes unless I really need to do inline string replacement. Yeah, so it's just a ridiculously tiny bit faster, but it is faster… And besides, you can type single quotes without hitting the Shift key, so it's a bit easier to type too.

And as for inline string replacement, I like to put the variable names in curly braces. I think it looks less messy and keeps the variable part of the string clearly defined. For example:

<?php
$var
= "You have {$coins} coins.";
?>