Posted in PHP

PHP: Implode or Trim – Concatenating strings in a loop

There are bunch of questions circulating in Stack Overflow like this. People when trying to concatenate some strings with another string or a character, they simply concatenate with . inside the loop like this:

$values = [1, 2, 3];
$string = '';
foreach ($values as $value) {
$string .= $value . " AND ";
}
echo $string;

This leaves string like this:

1 AND 2 AND 3 AND

Then they come up with a question as how to remove the last AND.

Continue reading “PHP: Implode or Trim – Concatenating strings in a loop”

Advertisements