2
$foo= 'dudes';
$bar= 'whats up';
<?php echo 'hey,'.$foo.' '.$bar.'?'; ?>
<?php echo "hey, $foo $bar?"; ?>

is

<span style="color:#993333">
<?php echo "hey, $foo $bar?"; ?>
</span>

slower than

<span style="color:#339933">
<?php echo 'hey,'.$foo.' '.$bar.'?'; ?>
</span>

?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
soredive
  • 795
  • 1
  • 9
  • 25
  • 1
    There are a lot of discussions about this, I posted it as a bad practice here: http://stackoverflow.com/questions/4891301/top-bad-practices-in-php/4891366#4891366 You should read the comments on the third answer and read some links. Contains a lot of useful information! – gnur Feb 07 '11 at 09:41
  • 2
    There's a difference, but it's like shaving rubber off the tires of your car to go faster. It matters so much less than everything else that determines how fast your program runs that it's not something you *actually* ever think about. – Dan Grossman Feb 07 '11 at 09:45
  • 1
    its faster yes, but for me its also a mater of style - its just much more readable and helps you / other developers to find variables faster when syntax highlighting is activated – Hannes Feb 07 '11 at 09:50
  • See http://stackoverflow.com/search?q=single+double+quotes+php – Gordon Feb 07 '11 at 10:09

3 Answers3

5

Yes. It is slightly faster to use single quotes.

This is because when you use double quotes PHP has to parse to check if there are variables in there.

Speed difference in using inline strings vs concatenation in php5?

Community
  • 1
  • 1
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • 3
    Single quoted strings are also "parsed" to see if there are any escaped characters... and I've yet to see any evidence to prove that either single or double quoted is faster; all I ever hear is second-hand rumour and hearsay. – Mark Baker Feb 07 '11 at 09:48
  • The answer in that topic has a comment asking for some sort of source for "it is faster". The link provided there to http://www.phpbench.com/ seems to imply it doesn't really matter in the newer versions? So the question raised by @a-rex seems valid. Is there any 'proof' ? – Nanne Feb 07 '11 at 09:48
2

Nope it is not a bad habit nor slower.

However, when using PHP templating, try to avoid printing several variables at once, to keep things straight.
I'd make it this way:

hey, <?=$foo?> <?=$bar?>?

But it's still matter of style as there are no other reasons to prefer one over another

A note. It's a pity to see numerous answers from not-so-experienced participants, who speaking not from their own real life experience but just repeat some durable rumor, one after another.

Also, I'd say that the question itself has very poor reasoning. If you asks what is slower - you asks wrong question. For the most questions such difference just doesn't matter and just waste of time. Performance tuning is a process, not some "mysterious knowledge". One have to learn how to profile their app and get some basic experience to ask sensible performance related questions. Otherwise there will be no any good.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Yes. It is slightly slower to type constructs with single quotes.

There is no difference in runtime speeds. The only difference is in tokenizing, where single quotes are processed negligibly faster.

I've run a test once (PHP 5.3.2, amd64, linux) to measure some difference, very unscientifically. Single quotes were 14% faster, but only because I pumped 1,000,000,000,000 strings through eval. Otherwise it's hard to measure. And it is seriously irrelevant for real world applications, since you never have that many strings.

Hencewhy the use of single quotes also raises the perception of unintelligent design, when there is another syntax construct that's pretty optimized and way more readable.

mario
  • 144,265
  • 20
  • 237
  • 291