<p>(assume php5) consider</p>

<pre><code>&lt;?php

    $foo = 'some words';

    //case 1
    print "these are $foo";

    //case 2
    print "these are {$foo}";

    //case 3
    print 'these are ' . $foo;
?&gt;
</code></pre>

<p>Is there much of a difference between 1 and 2?</p>

<p>If not, what about between 1/2 and 3?</p>

[{'answer_id': 13665, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 7, 'selected': True, 'text': '<p>Well, as with all "What might be faster in real life" questions, you can\'t beat a real life test.</p>\n\n<pre><code>function timeFunc($function, $runs)\n{\n  $times = array();\n\n  for ($i = 0; $i &lt; $runs; $i++)\n  {\n    $time = microtime();\n    call_user_func($function);\n    $times[$i] = microtime() - $time;\n  }\n\n  return array_sum($times) / $runs;\n}\n\nfunction Method1()\n{ \n  $foo = \'some words\';\n  for ($i = 0; $i &lt; 10000; $i++)\n    $t = "these are $foo";\n}\n\nfunction Method2()\n{\n  $foo = \'some words\';\n  for ($i = 0; $i &lt; 10000; $i++)\n    $t = "these are {$foo}";\n}\n\nfunction Method3()\n {\n  $foo = \'some words\';\n  for ($i = 0; $i &lt; 10000; $i++)\n    $t = "these are " . $foo;\n}\n\nprint timeFunc(\'Method1\', 10) . "\\n";\nprint timeFunc(\'Method2\', 10) . "\\n";\nprint timeFunc(\'Method3\', 10) . "\\n";\n</code></pre>\n\n<p>Give it a few runs to page everything in, then...</p>\n\n<p>0.0035568</p>\n\n<p>0.0035388</p>\n\n<p>0.0025394</p>\n\n<p>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, <strong>after</strong> all the work to parse out the variable and trim/copy up the original string.</p>\n\n<p><strong>Updates By Somnath:</strong></p>\n\n<p>I added Method4() to above real time logic.</p>\n\n<pre><code>function Method4()\n {\n  $foo = \'some words\';\n  for ($i = 0; $i &lt; 10000; $i++)\n    $t = \'these are \' . $foo;\n}\n\nprint timeFunc(\'Method4\', 10) . "\\n";\n\nResults were:\n\n0.0014739\n0.0015574\n0.0011955\n0.001169\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 13680, 'author': 'Pierre Spring', 'author_id': 1532, 'author_profile': 'https://Stackoverflow.com/users/1532', 'pm_score': 4, 'selected': False, 'text': '<p>@Adam\'s test used</p>\n\n<pre><code>"these are " . $foo\n</code></pre>\n\n<p>note that the following is even faster:</p>\n\n<pre><code>\'these are \' . $foo;\n</code></pre>\n\n<p>this is due to the fact, that a double quoted "string" gets evaluated, where a single quoted \'string\' is just taken as is...</p>\n'}, {'answer_id': 16128, 'author': 'Jake McGraw', 'author_id': 302, 'author_profile': 'https://Stackoverflow.com/users/302', 'pm_score': 4, 'selected': False, 'text': "<p>Don't get too caught up on trying to optimize string operations in PHP. Concatenation vs. interpolation is meaningless (in real world performance) if your database queries are poorly written or you aren't using any kind of caching scheme. Write your string operations in such a way that debugging your code later will be easy, the performance differences are negligible.</p>\n\n<p>@uberfuzzy Assuming this is just a question about language minutia, I suppose it's fine. I'm just trying to add to the conversation that comparing performance between single-quote, double-quote and heredoc in real world applications in meaningless when compared to the real performance sinks, such as poor database queries.</p>\n"}, {'answer_id': 482204, 'author': 'Paolo Bergantino', 'author_id': 16417, 'author_profile': 'https://Stackoverflow.com/users/16417', 'pm_score': 7, 'selected': False, 'text': '<p>The performance difference has been <a href="http://nikic.github.com/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html" rel="noreferrer">irrelevant</a> since at least January 2012, and likely earlier:</p>\n\n<pre><code>Single quotes: 0.061846971511841 seconds\nDouble quotes: 0.061599016189575 seconds\n</code></pre>\n\n<p>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:</p>\n\n<blockquote>\n  <p>Never trust a statistic you didn’t forge yourself.</p>\n</blockquote>\n\n<p>(Although the article quotes the phrase, the original quip was likely falsely <a href="http://www.statistik.baden-wuerttemberg.de/Service/Veroeff/Monatshefte/20041111.mha" rel="noreferrer">attributed</a> to Winston Churchill, invented by Joseph Goebbels\' propaganda ministry to portray Churchill as a liar:</p>\n\n<blockquote>\n  <p>Ich traue keiner Statistik, die ich nicht selbst gefälscht habe.</p>\n</blockquote>\n\n<p>This loosely translates to, "I do not trust a statistic that I did not fake myself.")</p>\n'}, {'answer_id': 482239, 'author': 'navitronic', 'author_id': 46264, 'author_profile': 'https://Stackoverflow.com/users/46264', 'pm_score': 2, 'selected': False, 'text': "<p>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. </p>\n\n<p>I can't seem to track down a link to the discussion at the moment though.</p>\n"}, {'answer_id': 482291, 'author': 'Mike B', 'author_id': 46675, 'author_profile': 'https://Stackoverflow.com/users/46675', 'pm_score': 5, 'selected': False, 'text': '<p>Live benchmarks:</p>\n\n<p><a href="http://phpbench.com/" rel="noreferrer">http://phpbench.com/</a></p>\n\n<p>There is actually a subtle difference when concatenating variables with single vs double quotes.  </p>\n'}, {'answer_id': 482318, 'author': 'kimsk', 'author_id': 58905, 'author_profile': 'https://Stackoverflow.com/users/58905', 'pm_score': 1, 'selected': False, 'text': '<p>Double quotes can be much slower. I read from several places that that it is better to do this</p>\n\n<pre><code>\'parse me \'.$i.\' times\'\n</code></pre>\n\n<p>than</p>\n\n<pre><code>"parse me $i times"\n</code></pre>\n\n<p>Although I\'d say the second one gave you more readable code.</p>\n'}, {'answer_id': 1435564, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>also, what is the memory situation of the server?  typically memory management on a higher level platform is worse than that at lower platforms... </p>\n\n<pre><code>$a = \'parse\' . $this; \n</code></pre>\n\n<p>is managing memory at the user code platform level...</p>\n\n<pre><code>$a = "parse $this";\n</code></pre>\n\n<p>is managing memory at the php system code platform level...</p>\n\n<p>so these benchmarks as related to CPU don\'t tell the full story.  </p>\n\n<p>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.</p>\n'}, {'answer_id': 6158187, 'author': 'Klerk', 'author_id': 773811, 'author_profile': 'https://Stackoverflow.com/users/773811', 'pm_score': 0, 'selected': False, 'text': '<p>Practically there is no difference at all! See the timings: <a href="http://micro-optimization.com/single-vs-double-quotes" rel="nofollow">http://micro-optimization.com/single-vs-double-quotes</a> </p>\n'}, {'answer_id': 11107091, 'author': 'Gordon', 'author_id': 208809, 'author_profile': 'https://Stackoverflow.com/users/208809', 'pm_score': 3, 'selected': False, 'text': '<p>Any differences in execution time are completely negligible.</p>\n\n<p>Please see</p>\n\n<ul>\n<li><a href="http://nikic.github.com/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html" rel="noreferrer">NikiC\'s Blog: Disproving the Single Quotes Performance Myth</a> for a technical explanation how interpolation and concatenation works in PHP and why it is absolutely pointless to care about their speed.</li>\n</ul>\n\n<p>Don\'t waste time on micro-optimizations like this. Use a profiler to measure the performance of your application in a real world scenario and then optimize where it is really needed. Optimising a single sloppy DB query is likely to make a bigger performance improvement than applying micro-optimisations all over your code.</p>\n'}, {'answer_id': 12856650, 'author': 'Rob Forrest', 'author_id': 236755, 'author_profile': 'https://Stackoverflow.com/users/236755', 'pm_score': 2, 'selected': False, 'text': '<p>Just to add something else to the mix, if you are using a variable inside a double quoted string syntax:</p>\n\n<pre><code>$foo = "hello {$bar}";\n</code></pre>\n\n<p>is faster than</p>\n\n<pre><code>$foo = "hello $bar";\n</code></pre>\n\n<p>and both of these are faster than</p>\n\n<pre><code>$foo = \'hello\' . $bar; \n</code></pre>\n'}, {'answer_id': 43625364, 'author': 'ywarnier', 'author_id': 6499848, 'author_profile': 'https://Stackoverflow.com/users/6499848', 'pm_score': 0, 'selected': False, 'text': '<p>It should be noted that, when using a modified version of the example by Adam Wright with 3 variables, the results are reversed and the first two functions are actually faster, consistently. This is with PHP 7.1 on CLI:</p>\n\n<pre><code>function timeFunc($function, $runs)\n{\n    $times = array();\n\n    for ($i = 0; $i &lt; $runs; $i++)\n    {\n        $time = microtime();\n        call_user_func($function);\n        @$times[$i] = microtime() - $time;\n    }\n\n    return array_sum($times) / $runs;\n}\n\nfunction Method1()\n{ \n    $foo = \'some words\';\n    $bar = \'other words\';\n    $bas = 3;\n    for ($i = 0; $i &lt; 10000; $i++)\n         $t = "these are $foo, $bar and $bas";\n}\n\nfunction Method2()\n{\n    $foo = \'some words\';\n    $bar = \'other words\';\n    $bas = 3;\n    for ($i = 0; $i &lt; 10000; $i++)\n         $t = "these are {$foo}, {$bar} and {$bas}";\n}\n\nfunction Method3()\n{\n    $foo = \'some words\';\n    $bar = \'other words\';\n    $bas = 3;\n    for ($i = 0; $i &lt; 10000; $i++)\n         $t = "these are " . $foo . ", " . $bar . " and " .$bas;\n}\n\nprint timeFunc(\'Method1\', 10) . "\\n";\nprint timeFunc(\'Method2\', 10) . "\\n";\nprint timeFunc(\'Method3\', 10) . "\\n";\n</code></pre>\n\n<p>I\'ve also tried with \'3\' instead of just the integer 3, but I get the same kind of results.</p>\n\n<p>With $bas = 3:</p>\n\n<pre><code>0.0016254\n0.0015719\n0.0019806\n</code></pre>\n\n<p>With $bas = \'3\':</p>\n\n<pre><code>0.0016495\n0.0015608\n0.0022755\n</code></pre>\n\n<p>It should be noted that these results vary highly (I get variations of about 300%), but the averages seem relatively steady and almost (9 out of 10 cases) always show a faster execution for the 2 first methods, with Method 2 always being slightly faster than method 1.</p>\n\n<p>In conclusion: what is true for 1 single operation (be it interpolation or concatenation) is not always true for combined operations.</p>\n'}, {'answer_id': 62986215, 'author': 'Stackoverflow', 'author_id': 7180968, 'author_profile': 'https://Stackoverflow.com/users/7180968', 'pm_score': 0, 'selected': False, 'text': '<p>Yes, originally this is about PHP5, however in few months arrive PHP8 and today the best option tested over my <strong>PHP 7.4.5</strong> is use <a href="https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc" rel="nofollow noreferrer">PHP - Nowdoc</a> (tested over WIN 10 + Apache and CentOs 7 + Apache):</p>\n<pre><code>function Method6(){\n    $k1 = \'AAA\';\n    for($i = 0; $i &lt; 10000; $i ++)$t = &lt;&lt;&lt;\'EOF\'\nK1= \nEOF\n.$k1.\n&lt;&lt;&lt;\'EOF\'\nK2=\nEOF\n.$k1;\n    }\n</code></pre>\n<p>here the method #5 (using <strong>Heredoc</strong> to concatenat):</p>\n<pre><code>function Method5(){\n    $k1 = \'AAA\';\n    for($i = 0; $i &lt; 10000; $i ++)$t = &lt;&lt;&lt;EOF\nK1= $k1\nEOF\n.&lt;&lt;&lt;EOF\nK2=$k1 \nEOF;\n    }\n</code></pre>\n<p>the methods 1 to 4 is in beginning of this post</p>\n<p>In all my tests the &quot;winner&quot; is method #6 (Newdoc), no\'t very easy to read, but very fast in CPU and ever using the function <code>function timeFunc($function)</code> by <strong>@Adam Wright</strong>.</p>\n'}, {'answer_id': 68206483, 'author': 'Rinshan Kolayil', 'author_id': 11543253, 'author_profile': 'https://Stackoverflow.com/users/11543253', 'pm_score': 0, 'selected': False, 'text': "<p>I have tested php 7.4 and php 5.4 with following test cases, It was little still confusing to me.</p>\n<pre><code>&lt;?php\n$start_time = microtime(true);\n$result = &quot;&quot;;\nfor ($i = 0; $i &lt; 700000; $i++) {\n    $result .= &quot;THE STRING APPENDED IS &quot; . $i;\n    // AND $result .= 'THE STRING APPENDED IS ' . $i;\n    // AND $result .= &quot;THE STRING APPENDED IS $i&quot;;\n}\necho $result;\n$end_time = microtime(true);\necho &quot;&lt;br&gt;&lt;br&gt;&quot;;\necho ($end_time - $start_time) . &quot; Seconds&quot;;\n</code></pre>\n<p>PHP 7.4 Outputs</p>\n<pre><code> 1. &quot;THE STRING APPENDED IS &quot; . $i = 0.16744208335876\n 2. 'THE STRING APPENDED IS ' . $i = 0.16724419593811\n 3. &quot;THE STRING APPENDED IS $i&quot; = 0.16815495491028\n</code></pre>\n<p>PHP 5.3 Outputs</p>\n<pre><code> 1. &quot;THE STRING APPENDED IS &quot; . $i = 0.27664494514465\n 2. 'THE STRING APPENDED IS ' . $i = 0.27818703651428\n 3. &quot;THE STRING APPENDED IS $i&quot; = 0.28839707374573\n</code></pre>\n<p>I have tested so many times, In php 7.4 it seems to be all 3 test cases got same result many times but still concatenation have little bittle advantage in performance.</p>\n"}, {'answer_id': 68391660, 'author': 'Meloman', 'author_id': 2282880, 'author_profile': 'https://Stackoverflow.com/users/2282880', 'pm_score': 0, 'selected': False, 'text': "<p>Based on @adam-wright answer, I wanted to know if speed difference happens without no concataining / no vars in a string.</p>\n<p><strong>== My questions...</strong></p>\n<ul>\n<li>is <code>$array['key']</code> call or set faster than <code>$array[&quot;key&quot;]</code> !?</li>\n<li>is <code>$var = &quot;some text&quot;;</code> slower than <code>$var = 'some text';</code> ?</li>\n</ul>\n<p><strong>== My tests</strong> with new vars every time to avoid use same memory address :</p>\n<pre><code>function getArrDblQuote() { \n    $start1 = microtime(true);\n    $array1 = array(&quot;key&quot; =&gt; &quot;value&quot;);\n    for ($i = 0; $i &lt; 10000000; $i++)\n        $t1 = $array1[&quot;key&quot;];\n    echo microtime(true) - $start1;\n}\nfunction getArrSplQuote() {\n    $start2 = microtime(true);\n    $array2 = array('key' =&gt; 'value');\n    for ($j = 0; $j &lt; 10000000; $j++)\n        $t2 = $array2['key'];\n    echo microtime(true) - $start2;\n}\n\nfunction setArrDblQuote() { \n    $start3 = microtime(true);\n    for ($k = 0; $k &lt; 10000000; $k++)\n        $array3 = array(&quot;key&quot; =&gt; &quot;value&quot;);\n    echo microtime(true) - $start3;\n}\nfunction setArrSplQuote() {\n    $start4 = microtime(true);\n    for ($l = 0; $l &lt; 10000000; $l++)\n        $array4 = array('key' =&gt; 'value');\n    echo microtime(true) - $start4;\n}\n\nfunction setStrDblQuote() { \n    $start5 = microtime(true);\n    for ($m = 0; $m &lt; 10000000; $m++)\n        $var1 = &quot;value&quot;;\n    echo microtime(true) - $start5;\n}\nfunction setStrSplQuote() {\n    $start6 = microtime(true);\n    for ($n = 0; $n &lt; 10000000; $n++)\n        $var2 = 'value';\n    echo microtime(true) - $start6;\n}\n\nprint getArrDblQuote() . &quot;\\n&lt;br&gt;&quot;;\nprint getArrSplQuote() . &quot;\\n&lt;br&gt;&quot;;\nprint setArrDblQuote() . &quot;\\n&lt;br&gt;&quot;;\nprint setArrSplQuote() . &quot;\\n&lt;br&gt;&quot;;\nprint setStrDblQuote() . &quot;\\n&lt;br&gt;&quot;;\nprint setStrSplQuote() . &quot;\\n&lt;br&gt;&quot;;\n</code></pre>\n<p><strong>== My Results :</strong></p>\n<p>array get <strong>double</strong> quote <strong>2.1978828907013</strong></p>\n<p>array get <strong>single</strong> quote <strong>2.0163490772247</strong></p>\n<p>array set <strong>double</strong> quote <strong>1.9173440933228</strong></p>\n<p>array get <strong>single</strong> quote <strong>1.4982950687408</strong></p>\n<p>var set <strong>double</strong> quote <strong>1.485809803009</strong></p>\n<p>var set <strong>single</strong> quote <strong>1.3026781082153</strong></p>\n<p><strong>== My conclusion !</strong></p>\n<p>So, result is that difference is not very significant. However, on a big project, I think it can make the difference !</p>\n"}]
['https://Stackoverflow.com/questions/13620', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/314/']