(assume php5) consider
<?php $foo = 'some words'; //case 1 print "these are $foo"; //case 2 print "these are {$foo}"; //case 3 print 'these are ' . $foo; ?>
Is there much of a difference between 1 and 2?
If not, what about between 1/2 and 3?
In PHP, people use single quote to define a constant string, like 'a' , 'my name' , 'abc xyz' , while using double quote to define a string contain identifier like "a $b $c $d" . echo "my $a"; This is true for other used of string.
I would say that single quotes are faster because they don't require Shift ;) The different quotes have implications on variable output and escape characters. Content inside single quotes is taken as is, no escape characters and variables are interpreted.
If you need to put a quotation inside your first quotation, use the opposite type of quotation marks to surround it. That's single quotation marks in American English. Double quotation marks can also be used to show sarcasm or to identify words used as words instead of for their meaning.
As far as language syntax is concerned, there is no difference in single or double quoted string. Both representations can be used interchangeably. However, if either single or double quote is a part of the string itself, then the string must be placed in double or single quotes respectively.
The performance difference has been irrelevant since at least January 2012, and likely earlier:
Single quotes: 0.061846971511841 seconds Double quotes: 0.061599016189575 seconds
Earlier versions of PHP may have had a difference - I personally prefer single quotes to double quotes, so it was a convenient difference. The conclusion of the article makes an excellent point:
Never trust a statistic you didn’t forge yourself.
(Although the article quotes the phrase, the original quip was likely falsely attributed to Winston Churchill, invented by Joseph Goebbels' propaganda ministry to portray Churchill as a liar:
Ich traue keiner Statistik, die ich nicht selbst gefälscht habe.
This loosely translates to, "I do not trust a statistic that I did not fake myself.")
Well, as with all "What might be faster in real life" questions, you can't beat a real life test.
function timeFunc($function, $runs) { $times = array(); for ($i = 0; $i < $runs; $i++) { $time = microtime(); call_user_func($function); $times[$i] = microtime() - $time; } return array_sum($times) / $runs; } function Method1() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = "these are $foo"; } function Method2() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = "these are {$foo}"; } function Method3() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = "these are " . $foo; } print timeFunc('Method1', 10) . "\n"; print timeFunc('Method2', 10) . "\n"; print timeFunc('Method3', 10) . "\n";
Give it a few runs to page everything in, then...
0.0035568
0.0035388
0.0025394
So, as expected, the interpolation are virtually identical (noise level differences, probably due to the extra characters the interpolation engine needs to handle). Straight up concatenation is about 66% of the speed, which is no great shock. The interpolation parser will look, find nothing to do, then finish with a simple internal string concat. Even if the concat were expensive, the interpolator will still have to do it, after all the work to parse out the variable and trim/copy up the original string.
Updates By Somnath:
I added Method4() to above real time logic.
function Method4() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = 'these are ' . $foo; } print timeFunc('Method4', 10) . "\n"; Results were: 0.0014739 0.0015574 0.0011955 0.001169
When you are just declaring a string only and no need to parse that string too, then why to confuse PHP debugger to parse. I hope you got my point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With