PHP Single vs. Double Quotes

For a long time, I’ve been a proponent of using single-quotes as opposed to double-quotes when assigning string literals (strings containing no variables) to a variable (eg: $var = ‘string’; VS $var = “string”). I just did some research to find out how much (if any) impact using single-vs-double quotes had on performance.

The results were fairly consistent: Time 1: 5.9485120773315 ($c = “test ” . $i; ) Time 2: 7.0326972007751 ($c = “test $i”; ) Time 3: 5.9164550304413 ($c = ‘test ‘ . $i; ) It’s clear that embedding variables in strings is less efficient, but the difference between normal single-vs-double quotes is negligible (at least on our server configuration with this test). Despite the lack of clear performance benefits, I still think it is best practice to use single-quotes when using string literals, as it denotes that a string doesn’t contain any variables. One benefit of this is that when skimming code, you can more quickly process which variables are string literals vs strings with embedded variables. The Zend Framework Coding Standard also recommends this practice:http://framework.zend.com/manual/en/coding-standard.coding-style.html

Share it

Topics

Related Posts

What ROAS Really Means

Everything You Need to Know About Updating to Google Analytics 4

Classy Llama is Proud to be a Sponsor of Signifyd’s #FLOWSummit2023

Contact Us