Is Zero Empty in PHP?
When you write $var = 0; followed by if (!empty($var)), expecting it to be "not empty" because a variable exists, you are running into one of PHP's most notorious behaviors: the intersection between truthiness and emptiness. The output "Its empty" might feel counterintuitive if you view variables purely as containers for data. However, empty() in PHP does not merely check if a variable has been assigned; it checks if that assignment results in a value that is considered "falsy" according to specific rules defined in the language specification.
In the provided code snippet, $var holds an integer 0. While this is a valid numeric value and distinct from null, PHP's empty() function treats 0, 0.0, and false as empty states. This design stems from the fact that these values are effectively null in logical operations within C-based languages, which PHP inherited. If your logic relies on the presence of a variable versus its value, relying solely on empty() will cause you to discard legitimate zeros.
Consider a scenario where you are validating user input for an age field. A user enters "0". Technically, they have provided input. The variable is set. Yet, running if (!empty($age)) would evaluate to false, potentially triggering error handling meant for missing fields rather than invalid data. This distinction is crucial when building robust forms or processing financial figures where zero is a valid, non-empty state representing a specific quantity.
To handle this correctly, you must separate the concept of variable existence from value truthiness. If your goal is strictly to check if a variable has been initialized and contains any value, including zero, use isset() instead. The function isset($var) returns true for $var = 0 because the key exists in the array or scope regardless of its value being falsy. This allows you to distinguish between "variable never set" and "variable set to zero".
However, there is a significant trade-off when using isset(): it will also return false if the variable has been explicitly set to null. In many strict type-checking environments, null is semantically different from 0, but both are treated as "empty" by isset(). If you need to differentiate between null and 0, you must use a combination of checks or direct comparison. For instance, $var !== null confirms the variable exists and isn't null, while keeping in mind that 0 is still falsy in a boolean context if you were to cast it later.
Another common pitfall involves arrays. The empty() function returns true for an array with no elements, but also for an array containing only false values like ['', 0, false]. If you are iterating over data structures and need to ensure keys exist before accessing them, array_key_exists() or the null coalescing operator ?? provides more granular control. The null coalescing operator is particularly useful here: $value = $var ?? 'default' will only substitute the default if $var is not set or is null, preserving the value of 0.
For developers working with Laravel, this distinction often impacts form validation and event listeners. While the core PHP behavior remains consistent, frameworks like https://laravelcompany.com sometimes encourage specific patterns to handle these edge cases more gracefully. For example, using Laravel's built-in helpers or castable traits can abstract away the need for manual empty() checks on integers, ensuring that a zero balance or count is respected rather than treated as an absence of data.
Ultimately, the decision between empty() and isset() depends on your semantic intent. If you care about the logical value of the variable, empty() is inappropriate for numeric zeros. If you care strictly about the existence of the key in a scope, isset() is the safer bet. Understanding that 0 is not an empty string or null, but a valid number treated as false by empty(), prevents logic errors in critical branches of your application.
