6

I'm working on a big project in php and I need to make sure it's all fast. So I'm wondering: what is faster to use, " or ' ? (Eg: $_SESSION['example'] or $_SESSION["example"])

Marnix Bouhuis
  • 430
  • 1
  • 6
  • 15
  • 2
    everything within " " wil be analysed and everything within ' ' not – Grumpy Dec 14 '15 at 15:07
  • 7
    Write a benchmark and test it. In my experience, `"` is very slightly (~4%) faster. But note: this is probably a case of very premature optimization. – elixenide Dec 14 '15 at 15:10
  • 1
    possible duplicate http://stackoverflow.com/a/13680/5515060 – Lino Dec 14 '15 at 15:12
  • 10
    @Grumpy - not true, even strings inside `'` need to be analysed for escape characters – Mark Baker Dec 14 '15 at 15:12
  • 1
    On a big project - It is most likely that database access will be a performance issue rather than parsing strings natively in PHP (which is a none issue anyway)? – Ryan Vincent Dec 14 '15 at 15:23
  • You might save a moment when typing single quoted keys, as you won't have to hit the shift key. (Edit: true on US Qwerty layout, not on every layout i.e. Azerty.) – Progrock Dec 14 '15 at 15:26
  • That sounds like an interesting project to work on. There is a lot to be learned from working on that project. Thanks for the update - I think you will have a lot of fun and interesting times - always try an enjoy it. – Ryan Vincent Dec 14 '15 at 21:55

2 Answers2

10

You shouldn't even care about this. It makes no real difference. No practical impact.

http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html

https://speakerdeck.com/dshafik/phpaustralia-2015-php-under-the-hood


Lets use the same benchmark data from the post by Mark Smit:

For a real speed benchmarks between quotes you can look at http://www.phpbench.com/

Q: Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x

A: 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!

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
-5

You should use ' because everything within " is analized, which makes it perform a lot slower. Right now magic quotes are even depricated. More information about that can be found on the php website

For a real speed benchmarks between quotes you can look at phpbench

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Mark Smit
  • 582
  • 4
  • 12