What are some common pitfalls when using IF and Foreach statements in PHP?

One common pitfall when using IF and Foreach statements in PHP is not properly checking if the variable being looped through is an array before using a Foreach loop. This can lead to errors if the variable is not an array. To avoid this, always use the is_array() function to check if the variable is an array before using a Foreach loop.

// Check if variable is an array before using Foreach loop
if(is_array($array_variable)){
    foreach($array_variable as $value){
        // Code to execute for each element in the array
    }
} else {
    // Handle the case where the variable is not an array
}