DEV Community

loading...

Discussion on: 4 PHP Tricks to Boost Script Performance

Collapse
diegomgar profile image
Dieg Oto

I readed somewhere that double quotes are more than double fast, it really surprised me.

Collapse
stojakovic99 profile image
Nikola Stojaković

Well, it's not that surprising, considering double quotes have to process the content.

Collapse
diegomgar profile image
Dieg Oto

Re read, you will see the surprise!

Collapse
diegomgar profile image
Dieg Oto

There it is, simple quote are faster only when it creates an empty string xD
phpbench.com/

Collapse
devmount profile image
Andreas Author

It looks like there is really no significant difference. See this tweet.

Collapse
devmount profile image
Andreas Author

Thank you for this link, this was tested with PHP 7.2, I will retest with the current version of PHP and will present the result 👍🏻

Collapse
devmount profile image
Andreas Author

Double quotes appear to be slower than single quotes because PHP parses their content for variables, e.g.: "a house with $n windows", but on the other hand, single quotes are parsed as well, e.g. 'a \'quoted\' string'

Collapse
diegomgar profile image
Dieg Oto

I'm looking for the source, that's why it surprised me, it has no sense.

Collapse
devmount profile image
Andreas Author

I can confirm that the single vs. double quotes myth is really just a myth. At least for the PHP 7.2+ (maybe this originates from very early PHP versions). I did a quick benchmark:

You can say with caution, that in average, double quotes are slightly (and I really mean very slightly!) faster than single quotes. We're talking about a max of 2ms on 5 million iterations... Obviously only, if no variable is replaced.