Skip to content
PHP Logo

What potential error can occur when using implode() on an empty array in PHP?

When using implode() on an empty array in PHP, a potential error that can occur is a "Warning: implode(): Invalid arguments passed" message. This happens because implode() expects at least one parameter to be passed to it, and an empty array does not meet this requirement. To solve this issue, you can check if the array is empty before calling implode() to avoid the warning.

php
// Check if the array is empty before using implode()
if (!empty($array)) {
    $result = implode(", ", $array);
    echo $result;
} else {
    echo "Array is empty";
}