Skip to content
PHP Logo

What are some alternative methods to check if an array is empty in PHP before using implode()?

When using the implode() function in PHP to concatenate array elements into a string, it's important to check if the array is empty to avoid errors. One way to do this is by using the empty() function to check if the array is empty before calling implode(). This ensures that implode() is only used when there are elements in the array to concatenate.

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