$string = '';
foreach ($fields as $key => $value) {
// don't include password
if ('password' != $key) {
$string .= ",<b>$value</b>";
}
}
$string = substr($string, 1); // remove leading ","
Notice that a separator is always added to each value, then stripped
off outside the loop. While it's somewhat wasteful
to add something that will be later subtracted, it's
far cleaner and efficient (in most cases) then attempting to embed
logic inside of the loop. To wit:
$string = '';
foreach ($fields as $key => $value) {
// don't include password
if ('password' != $value) {
if (!empty($string)) { $string .= ','; }
$string .= "<b>$value</b>";
}
}
Now you have to check $string every time you
append a value. That's worse than the simple
substr( ) call. Also, prepend the delimiter (in
this case a comma) instead of appending it because
it's faster to shorten a string from the front than
the rear.