What potential pitfalls should be considered when concatenating strings in PHP arrays?

When concatenating strings in PHP arrays, it's important to ensure that the values being concatenated are actually strings. If non-string values are present in the array, they will be converted to strings before concatenation, which may lead to unexpected results. To avoid this issue, you can explicitly cast array values to strings before concatenating them.

$array = [1, 2, '3', 4];
$result = '';

foreach ($array as $value) {
    $result .= (string)$value;
}

echo $result;