Hi pickle, did you read my post and follow that link? [php_man]implode[/php_man]() does exactly what you did in that foreach loop, look:
Code: Select all
<?php
$array = range(1, 5); // [php_man]range[/php_man]()
$string = implode('', $array); // [php_man]implode[/php_man]()
print_r($array); // [php_man]print_r[/php_man]()
echo $string . "\n";
?>
what outputs:
Code: Select all
Array
(
ї0] => 1
ї1] => 2
ї2] => 3
ї3] => 4
ї4] => 5
)
12345
Of course, your example is correct, and I understand that it is a good example to show an apprentice or beginner how things work and how he can develop his own techniques.
But, most of the times, this doesn't play a role. Commonly we are in a production scenery. Imagine that we have to do this several times in multimple parts of our application. We better place this peace of code inside a function and call it whenever we need it.
In this case, [php_man]implode[/php_man]() does the un-exploding job in an easier (that part would be played by our function), clearer (just a function with couple args), shorter (one line) and faster (the code is already compiled and linked inside php lib) manner. Not to mention that the code would be more compatible and already documented.
PHP has many functions that perform tasks like this, hackneyed, common tasks of ours day by day.
Regards,
Scorphus.