String concatenation in PHP (with a variable inside of it) , which one has a better performance? -
i have these 2 codes in php:
$msgs = 5; //these 2 types of string concatenation echo 'you got ' . $msgs . ' messages'; echo "you got $msgs messages";
let's create new test, simple
live example
test<? $str1 = $str2 = ""; ($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; ($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") . " faster!"; result dotted: 1.1234998703003e-6 interpolated: 1.2600898742676e-6 dot faster! real fact the difference little significative, interpolation, elegant , faster read, you! ;)
php performance concatenation string-concatenation
No comments:
Post a Comment