(N.B. - this may be a well-known thing, but it was new to me). I was
reading a script written by someone else, and noticed a coding style
difference - they always concatenated variables into strings, while I
always keep them inline. Out of curiosity, I wrote the following (with
some help from the online php man page on microtime()) to see which was
more efficient. Not only is my way (in-line) worse, it's *much* worse.
If anyone has a different interpretation, let me know.
<?php
/* Values from our dev server:
Concat string (50,000 reps):
1.156
1.127
1.128
In-line string (50,000 reps):
3.524
3.312
3.287
*/
function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}
$val1 = 1;
$val2 = "Hello World";
$start = microtime();
for ($i = 0; $i < 50000; $i++) {
$string1 = "First part of string ".$val1." Second part of
string ".$val2." final part";
// $string2 = "First part of string $val1 Second part of string
$val2 final part";
}
$diff = microtime_diff($start, microtime());
$duration = sprintf("%0.3f", $diff);
echo "Processing took $duration seconds";
?>