What are the best practices for handling empty arrays and avoiding pitfalls like invalid arguments in PHP foreach loops?

When dealing with empty arrays in PHP foreach loops, it's important to check if the array is empty before attempting to iterate over it to avoid errors. One way to handle this is by using the `empty()` function to check if the array is empty before entering the foreach loop. This will prevent any invalid argument errors that may occur when trying to iterate over an empty array.

$myArray = [];

if(!empty($myArray)) {
    foreach($myArray as $item) {
        // do something with $item
    }
} else {
    echo "Array is empty";
}