PHP check if False or Null
Determining whether a variable holds a false value versus a null state is one of those subtle distinctions that trips up even seasoned developers working in PHP. While both represent "falsy" values in conditional logic, they are distinct types with different implications for application flow and data integrity. When a function returns from an API call or a database query, you often need to distinguish between "no data found" (null) and an explicit logical negation (false).
The confusion usually stems from trying to decide between empty(), isset(), and the strict comparison operator ===. Each of these tools has specific behaviors regarding type juggling that can lead to unexpected results if not applied correctly.
The Trap of empty()
Many developers reach for empty() as a universal catch-all for checking if something exists. However, this function is overly broad. It treats false, null, "" (empty string), 0, and "0" all as empty.
If your goal is strictly to distinguish between false and null, using empty() will fail because it returns true for both:
$varFalse = false;
$varNull = null;
var_dump(empty($varFalse)); // bool(true)
var_dump(empty($varNull)); // bool(true)
In this scenario, you lose the ability to tell which specific value was returned. If your function signature implies that false means "explicit failure" and null means "no record," collapsing them together is a logical error. You should generally avoid empty() unless you specifically need to handle all falsy values as a single group, such as checking if an optional configuration flag has been set at all.
Using isset() for Null Checks
The isset() function was designed specifically to check for the existence of variables that are not null. It returns false if the variable is unset or explicitly set to null.
var_dump(isset($varNull)); // bool(false)
This is useful when distinguishing between a variable that doesn't exist versus one that exists but contains no data. However, isset() does not care about false. If you have:
$varFalse = false;
isset($varFalse) will return true, because the variable is defined and holds a value (even if that value is false). Therefore, isset() alone cannot distinguish between false and other non-null values. It is strictly a "is set?" check, not an "is null?" check in the type-safety sense.
The Correct Approach: Strict Comparison
To accurately determine if a variable is null, you must use the loose equality operator with a cast or strict comparison. The most robust pattern for modern PHP code is using the === operator to check strictly against null. This avoids type juggling issues where null == false might yield unexpected results depending on version quirks, although in standard PHP 8+, null === false correctly returns false.
if ($returnedValue === null) {
// Handle the case where no data was returned
} elseif ($returnedValue === false) {
// Handle the case where an explicit failure occurred
}
Alternatively, you can use the type cast (bool) combined with a check, but strictly speaking, === null is the clearest intent. If you need to check for both at once, !isset() covers both null and unset, while !== false catches the specific boolean false state.
Practical Trade-offs in API Handling
When wrapping external APIs or database queries, the return type often dictates the logic. A common pattern in Laravel-like frameworks is to return null when a resource is not found, and false when an error occurs during retrieval.
If you use empty() here, your error handling becomes ambiguous. You might silently ignore an API failure because the returned false looks like an empty result. By switching to strict type checks, you ensure that your application reacts differently to "nothing found" versus "operation failed."
While libraries like those discussed on https://laravelcompany.com often provide helper methods to simplify these checks, understanding the underlying PHP mechanics ensures you don't rely blindly on abstractions that might change behavior across versions. Always verify how a specific method handles return types before assuming empty() is sufficient for your business logic.