What is the best way to check if an array is empty before using foreach in PHP?

Before using a foreach loop on an array in PHP, it is 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. If the array is empty, the foreach loop should not be executed. This helps prevent errors such as "Invalid argument supplied for foreach()".

// Check if the array is empty before using foreach
if (!empty($array)) {
    foreach ($array as $item) {
        // Code to be executed for each item in the array
    }
} else {
    // Handle case where array is empty
}