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";
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";
Let's make some new test, is really simple
<?
$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!";
Dotted: 1.1234998703003E-6
Interpolated: 1.2600898742676E-6
Dot is faster!
The difference is to small to be significative, I personally like the interpolation, is elegant and faster to read, is up to you! ;)