2

Is it true that PHP adds more processing for quoted strings because it searches for variables?

If so, is it better to use single quotes and break them up for variables. Or, what method is better?

Makyen
  • 31,849
  • 12
  • 86
  • 121
Famver Tags
  • 1,988
  • 2
  • 16
  • 18
  • I essentially agree with @Ben. I did some unscientific benchmarking on my own, and for _my purposes_ they were both pretty close. That's not to say that for a specific application one isn't better than the other, just as far as my use of it at that time was concerned. – RobertB Dec 31 '10 at 19:33
  • The difference is usually measured in *micro*seconds – CaseySoftware Dec 31 '10 at 20:10
  • premature optimizations is the root of all evil => http://c2.com/cgi/wiki?PrematureOptimization – Alfred Dec 31 '10 at 20:39

1 Answers1

2

Yes, since double quotes allow PHP to perform variable interpolation, there will be a (very very small, even I dare say almost theoretical) performance hit (tokenizer only) as it finds variables whose values to replace into the strings.

However, performance should not be the deciding factor when choosing between single quotes or double quotes. Readability would be a slightly better concern. That is, performance of one's brain would be affected more than performance of the interpreter.

mario
  • 144,265
  • 20
  • 237
  • 291
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    Also, said performance hit may very well be less than doing something like – Tyler Eaves Dec 31 '10 at 19:35
  • The parsing needs to be done anyway. The only difference is that in a single quoted string there are only two special characters (`\‍` and `'`) while in a double quoted string there are `\‍`, `"`, and additionally `$`, `{$` with more special cases following. – Gumbo Dec 31 '10 at 19:40
  • No there isn't. Proper tokenizers are O(n) state machines don't search for anything. [See PHP tokenizer's source code](http://gcov.php.net/PHP_5_3/lcov_html/var/php_gcov/PHP_5_3/Zend/zend_language_parser.c.gcov.php). – Kornel Dec 19 '11 at 20:22