-2

I have these two codes in PHP:

$msgs = 5;
//These two types of string concatenation

echo 'You got ' . $msgs . ' messages';

echo "You got $msgs messages";
TimonSeldenrijk
  • 499
  • 1
  • 4
  • 13
  • There are places online where you can test this. For such a low iteration the performance differences would be negligible. – Jay Blanchard Oct 03 '14 at 21:27
  • @JayBlanchard Which places then? Instead of -'ing me, help me. – TimonSeldenrijk Oct 03 '14 at 21:29
  • See this question: http://stackoverflow.com/questions/13620/speed-difference-in-using-inline-strings-vs-concatenation-in-php5 :) – Axel A. García Oct 03 '14 at 21:31
  • 2
    -'ing you? I'm not -'ing you. I *do* have to ask if you Googled for this info because it looks like you put no effort into finding the answer before coming here. – Jay Blanchard Oct 03 '14 at 21:34
  • Here, have a read of a [comprehensive study of various string-related optimizations](http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html). – i alarmed alien Oct 03 '14 at 22:27

1 Answers1

0

Let's make some new test, is really simple

Live example

Test

<?
$str1 = $str2 = "";

for ($i=0; $i < 10000; $i++) {
    $start = microtime(true);
    $str1 .= 'You got ' . $i . ' messages';
    $str1_test[] = microtime(true) - $start;
}

echo "Dotted: " . ($str1_result = array_sum($str1_test) / 10000);

echo PHP_EOL;

for ($i=0; $i < 10000; $i++) {
    $start = microtime(true);
    $str2 .= "You got {$i} messages";
    $str2_test[] = microtime(true) - $start;
}

echo "Interpolated: " . ($str2_result = array_sum($str2_test) / 10000);
echo PHP_EOL . ($str2_result < $str1_result ? "Interpolation" : "Dot") . " is faster!";

Result

Dotted: 1.1234998703003E-6
Interpolated: 1.2600898742676E-6
Dot is faster!

Real fact

The difference is to small to be significative, I personally like the interpolation, is elegant and faster to read, is up to you! ;)

Axel A. García
  • 683
  • 9
  • 21
  • 1
    Good answer, although I would say the differences are so negligible here, they'd get lost in the background noise of normal machine operation; ie: the difference would be within the margin of error. To add to this: there's cases wherein either approach make for more elegant code. If one is embedding dynamic values in a literal "template" string, interpolation is - IMO - more clear. If one has a number of strings to concatenate: use the concatenation operator. Use the most appropriate approach for the job, rather than worrying about negligible performance considerations. – Adam Cameron Oct 04 '14 at 06:37
  • @AdamCameron you are totally right. Also the interpolation doesn't work for complex operators/expresions :( – Axel A. García Oct 05 '14 at 01:10
  • 1
    Yeah, that sux a bit. Am just in the process of learning PHP (come from CFML), and have been bitten on the bum by that fairly frequently. CFML is more forgiving in that area than PHP. I'm erring towards using `printf()` / `sprintf()` in those situations, but still "it depends". – Adam Cameron Oct 05 '14 at 14:21