What's the difference between 'isset()' and '!empty()' in PHP?
ISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET
returns TRUE
if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE
for ISSET
.
EMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.
PHP: is there a difference between (isset($a) && !empty($a)) and $a??false
Firstly, let's understand what the different elements are doing:
isset
takes a variable, array element, etc, and returns true if it exists, and has any value other than nullempty
takes a variable, array element, etc, and returns false if it exists, and has any value which would be considered equivalent totrue
??
examines a variable, array element, etc, and returns its value if it exists, and has any value other than null; otherwise, it returns its right-hand argument- any expression used in an
if
statement is cast to boolean automatically
So, we can look at the equivalences:
empty
is a combination ofisset()
and a cast to false; specifically,empty($a) === !isset($a) || !(bool)$a
, so!empty($a) === isset($a) && (bool)$a
??
uses the same check asisset
, not the same check asempty
; so$a ?? false
on its own is equivalent toisset($a) ? $a : false
- but, in an
if
statement, the whole thing will be forced to boolean, soif ( $a ?? false ) ...
is equivalent toif ( (bool)(isset($a) ? $a : false) ) ...
or more readablyif ( isset($a) ? (bool)$a : false ) ...
Which brings us to the answer, which is that the following all do the same thing:
if ( isset($a) && !empty($a) ) ...
if ( isset($a) && !(bool)$a ) ... // empty() is just a bool cast + isset
if ( !empty($a) ) ... // the isset is actually redundant
if ( isset($a) ? (bool)$a : false ) ... // same logic as the && version
if ( (bool)($a ?? false) ) ... // ?? does the isset for us
if ( $a ?? false ) ... // if does the (bool) for us
Probably the most readable is just to use !empty($a)
on its own, and leave ??
for where you actually need the original value rather than a true
.
In where shall I use isset() and !empty()
isset vs. !empty
FTA:
"isset() checks if a variable has a
value including (False, 0 or empty
string), but not NULL. Returns TRUE
if var exists; FALSE otherwise.On the other hand the empty() function
checks if the variable has an empty
value empty string, 0, NULL or
False. Returns FALSE if var has a
non-empty and non-zero value."
PHP: Variable empty or not set or what?
if (isset($data)) {
Variable is just set - before that line we declared new variable with name 'data', i.e.
$data = 'abc';
if (!empty($data)) {
Variable is filled with data. It cannot have empty array because then $data
has array type but still has no data, i.e. $data = array(1);
Cannot be null, empty string, empty array, empty object, 0, etc.
if ($data != '') {
Variable is not an empty string. But also cannot be empty value (examples above).
If we want to compare types, use !==
or ===
.
if ($data) {
Variable is filled out with any data. Same thing as !empty($data)
.
Related Topics
Dollar ($) Sign in Password String Treated as Variable
PHP - How to Force Download of a File
Accessing Dates in PHP Beyond 2038
Dompdf and Set Different Font-family
Why Is Using a MySQL Prepared Statement More Secure Than Using the Common Escape Functions
PHP: Utilizing Exit(); or Die(); After Header("Location: ");
How to Prepend File to Beginning
PHP Namespace Simplexml Problems
How to Test If a MySQL Query Was Successful in Modifying Database Table Data
When to Use Static VS Instantiated Classes
Codeigniter: Create New Helper
PHP List All Files in Directory
PHPmailer Godaddy Server Smtp Connection Refused
Multiple Image Upload PHP Form With One Input
Why Are My PHP Files Showing as Plain Text