Best way to test for a variable's existence in PHP; isset() is clearly broken
If you have been writing PHP for any length of time, you know the standard pattern for checking if a variable exists: isset(). It feels intuitive. You check $var, and if it returns true, you proceed. But the documentation tells a different story: "isset() will return FALSE if testing a variable that has been set to NULL."
This distinction is critical, yet it often trips up developers. The function doesn't just ask, "Is this variable defined?" It asks, "Is this variable defined and not equal to null?" This creates a significant ambiguity when your application logic needs to differentiate between a missing key and a key explicitly set to NULL.
Consider an array representing data for an SQL UPDATE statement. In this scenario, the array keys map to table columns, and the values are the data to be applied. Every column in the database might accept a NULL value. You need a mechanism to distinguish between two states:
- The column should not be touched because it wasn't provided in the payload.
- The column should be explicitly updated to
NULL.
If you use isset() here, both scenarios return false. You cannot tell the difference between "key missing" and "value is null." This ambiguity breaks your logic immediately.
The common workaround seen in forums often looks like this:
if (isset($v) || @is_null($v)) {
// ...
}
The @ suppressor is necessary because accessing an unset variable triggers a warning. However, this approach relies on error suppression to handle the edge case, which is fragile. Furthermore, is_null() behaves oddly with unset variables; it often returns true even when the variable doesn't exist, compounding the confusion rather than solving it. Comparing $v === NULL directly suffers from the same issue regarding unset variables without the error suppression workaround.
We need a function that treats "unset" and "set to null" as mutually exclusive states. That function is array_key_exists().
While the name suggests it only works on arrays, its behavior extends logically to global variables and effectively acts as the robust equivalent for existence checks in those contexts. When you pass an associative array or the $GLOBALS superglobal to it, it returns true if the key is present in the array regardless of its value.
$a = NULL;
var_dump(array_key_exists('a', $GLOBALS)); // bool(true)
var_dump(array_key_exists('b', $GLOBALS)); // bool(false)
In this example, $a is set to NULL. If you were checking the key 'a' in a way that didn't account for null values, you might expect false. But array_key_exists confirms the key is there, distinguishing it from a key that has never been instantiated.
This same logic applies to object properties via property_exists(). Unlike isset(), which returns false if a property is set to null, property_exists() correctly identifies that the property exists on the class definition or instance, regardless of its current value.
For modern PHP development, specifically within the Laravel framework, this distinction matters even more. When building forms or handling request data, you frequently encounter scenarios where a field might be empty (which becomes null after validation) versus a field that simply isn't submitted. If you are constructing dynamic queries or updating specific model attributes based on an array of changes, using isset() can lead to silent data loss where valid NULL updates are ignored because the key appears "missing."
Instead of fighting the language's quirks with error suppression operators (@) or complex boolean logic, rely on the standard library functions designed for existence checks. Use array_key_exists() for arrays and $GLOBALS, and property_exists() for objects. These tools provide the semantic clarity needed to write robust code that respects the difference between absence and nullity.
