- implode: documentation ( source)
<?php
$foo = [1, 2, 3, 4];
$arr = [];
foreach($foo as $bar) {
try {
if($bar == 3) {
throw new Exception("error: " + $bar);
}
$arr[] = $bar;
} catch (Exception $e) {
$arr[] = $e->getMessage();
}
}
echo implode($arr, ',');