What are some common pitfalls to avoid when using if statements in PHP to check for the presence of a value in a variable?

One common pitfall when using if statements in PHP to check for the presence of a value in a variable is not using the strict comparison operator (===) to check for both the existence and the value of the variable. This can lead to unexpected behavior if the variable contains a falsy value like 0 or an empty string. To avoid this, always use the strict comparison operator to ensure that the variable is both set and has the expected value.

// Incorrect way to check for the presence of a value in a variable
if ($variable) {
    echo "Variable is set and has a value";
}

// Correct way to check for the presence of a value in a variable
if (isset($variable) && $variable !== null) {
    echo "Variable is set and has a value";
}