Are there any performance benefits to using single quotes instead of double quotes in php?
In other words, would there be a performance benefit of:
$foo = 'Test';
versus
$foo = "Test";
G-Man
Are there any performance benefits to using single quotes instead of double quotes in php?
In other words, would there be a performance benefit of:
$foo = 'Test';
versus
$foo = "Test";
G-Man
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.
So, if you do:
$bar = 'world';
$foo = "hello $bar";
$baz = 'hello $bar';
$foo
would contain "hello world" while $baz
could contain "hello $bar"
Whenever you are not using variables inside a string it's good practice to just use single quotes so PHP doesn't have to parse the string.
I seem to remember that the developer of the forum software, Vanilla replaced all the double quotes in his code with single quotes and noticed a reasonable amount of performance increase.
I can't seem to track down a link to the discussion at the moment though.
Live benchmarks:
There is actually a subtle difference when concatenating variables with single vs double quotes.
Double quotes can be much slower. I read from several places that that it is better to do this
'parse me '.$i.' times'
than
"parse me $i times"
Although I'd say the second one gave you more readable code.
there is a difference when concatenating variables... and what you are doing with the result... and if what you are doing is dumping it to output, is or isn't output buffering on.
also, what is the memory situation of the server? typically memory management on a higher level platform is worse than that at lower platforms...
$a = 'parse' . $this;
is managing memory at the user code platform level...
$a = "parse $this";
is managing memory at the php system code platform level...
so these benchmarks as related to CPU don't tell the full story.
running the benchmark 1000 times vs running the benchmark 1000 times on a server that is attempting to run that same simulation 1000 times concurrently... you might get drastically different results depending on the scope of the application.