3 ms·
Thankfully others have correctly identified this is not a real use case for empty and someone wrote an entire article about it. The value that empty() provides
by ddtaylor 2y ago
Thankfully others have correctly identified this is not a real use case for empty and someone wrote an entire article about it.
The value that empty() provides in PHP is similar to a null coalescing operator in many languages:
if (empty($foo->bar['baz'])) {
In this line of code all of the above can be missing. $foo can be an undefined variable, let alone one that has a value, and likewise all of the members, arrays, keys, etc. can also be missing. It does not throw an exception or trigger any warnings/errors to be logged, since, well, that's the purpose of empty.
If you want similar-ish behavior but you're not interested in the problems associated with the empty semantics, use isset() as it has the same properties for non-existent keys.