What are the best practices for handling arrays returned from functions in PHP?

When working with arrays returned from functions in PHP, it is best practice to assign the returned array to a variable and then check if the variable is an array before accessing its elements. This helps avoid errors in case the function does not return an array as expected.

// Assign the returned array from a function to a variable
$array = getArray();

// Check if the variable is an array before accessing its elements
if (is_array($array)) {
    // Access the elements of the array
    foreach ($array as $element) {
        // Do something with each element
    }
} else {
    // Handle the case where the function did not return an array
    echo "Error: Function did not return an array.";
}