Why check both isset() and !empty()
In PHP, checking if a variable exists before verifying its value is a common pattern, but the specific combination of isset() and !empty() often invites unnecessary complexity. Consider the expression:
isset($vars[1]) AND !empty($vars[1])
At first glance, this looks like a rigorous safety check. You are confirming that the index exists in the array and that it holds a non-empty value. However, from a logical standpoint, this double check is redundant. If you understand the definitions of both functions, you will see that isset() already implies !empty() for most practical scenarios involving arrays and strings.
The function isset($var) returns true if the variable is set and is not null. Conversely, !empty($var) returns true only if the variable evaluates to "false" in a boolean context. In PHP, null, 0, "0", "" (empty string), and [] (empty array) are all considered empty.
When you combine them with an AND operator, you are essentially asking: "Is the key set, but not null?" followed immediately by "Is it also not empty?" Since isset() already filters out null, the second part of your condition is only useful if you care about distinguishing between a null value and other falsy values like an empty string or zero.
If your goal is simply to ensure that $vars[1] contains data, the standard approach is often overkill. If you write:
if (isset($vars[1])) {
// Logic here
}
You are guaranteed that $vars[1] exists and is not null. If the value happens to be an empty string or zero, your code inside the block will still execute. This might be exactly what you want, or it might be a bug waiting to happen depending on your business logic.
However, if you specifically need to exclude empty strings and zeros as well, isset() alone is insufficient. In that case, using just !empty() is actually the safer choice because it handles both existence checks (ignoring undefined keys which would trigger an error without isset) and value checks.
if (!empty($vars[1])) {
// This block runs only if $vars[1] exists AND has a "truthy" value
}
Using !empty() inherently covers the existence check for arrays and variables that might not be set, provided you handle potential notice issues in older PHP versions or specific configurations. In modern PHP (7.4+), accessing an undefined array key triggers a warning if error reporting is enabled, but empty() handles this gracefully by returning true for non-existent keys.
Wait, that is incorrect. Let's re-evaluate the behavior of empty() on undefined keys. If $vars[1] does not exist, PHP will throw a warning in strict environments or silently return true depending on how the variable is accessed. Actually, empty() on an undefined index returns true.
Therefore, if you use !empty($vars[1]), it returns false if the key is missing or if the value is empty. This single function covers both bases: existence and content.
The only scenario where you would strictly need both checks is if you are dealing with a variable that might be explicitly set to null but you want to treat that as a valid state distinct from an empty string. But in standard validation flows, this distinction rarely adds value. Most of the time, developers use isset() to avoid warnings when accessing potentially missing keys, followed by logic that assumes a value exists.
If your application is built with Laravel, the framework provides robust helper functions like old() or strict validation rules that handle these edge cases automatically. For instance, using Laravel's validation engine ensures you don't have to manually juggle boolean flags for every input field. You can rely on the validator to check if a field is filled and not empty in one go.
Ultimately, the pattern isset($var) && !empty($var) simplifies down to just !empty($var) in 95% of use cases. If you need to handle null specifically as a valid input separate from an empty string, is_null() becomes relevant, but that is a specific requirement rather than a general best practice. Simplifying your conditions reduces cognitive load and minimizes the risk of logic errors where a developer forgets that isset() allows false, 0, or "" to pass through.
