I tend to avoid using loops in situations such as these. You should use implode() to join lists with a common deliminator and use array_map() to handle any processing on that array before the join. Note the following implementations that should express the versatility of these functions. Array map can take a string (name of a function) representing either a built-in function or user defined function (first 3 examples). You may pass it a function using create_function() or pass a lambda/anonymous function as the first parameter.
$a = array(1, 2, 3, 4, 5);
// Using the user defined function
function fn ($n) { return $n * $n; }
printf('[%s]', implode(', ', array_map('fn', $a)));
// Outputs: [1, 4, 9, 16, 25]
// Using built in htmlentities (passing additional parameter
printf('[%s]', implode(', ', array_map( 'intval' , $a)));
// Outputs: [1, 2, 3, 4, 5]
// Using built in htmlentities (passing additional parameter
$b = array('"php"', '"&<>');
printf('[%s]', implode(', ', array_map( 'htmlentities' , $b, array_fill(0 , count($b) , ENT_QUOTES) )));
// Outputs: ["php", "&<>]
// Using create_function <PHP 5
printf('[%s]', implode(', ', array_map(create_function('$n', 'return $n + $n;'), $a)));
// Outputs: [2, 4, 6, 8, 10]
// Using a lambda function (PHP 5.3.0+)
printf('[%s]', implode(', ', array_map(function($n) { return $n; }, $a)));
// Outputs: [1, 2, 3, 4, 5]