Current Location: Home> Latest Articles> Combining isset and is_null to determine whether a variable has been defined and null

Combining isset and is_null to determine whether a variable has been defined and null

gitbox 2025-05-29

The difference between isset() and is_null()

    • Used to determine whether the variable has been defined and the value is not null .

    • If the variable does not exist or the value of the variable is null , isset() returns false .

    • Note that isset() supports detecting multiple variables at once, and returns true only if all variables are defined and not null .

  • is_null()

    • Determine whether the value of the variable is null , and does not care whether the variable has been defined.

    • If the variable does not exist, a warning will be triggered (Notice: Undefined variable).


Combining isset and is_null to determine whether the variable is defined and null

Using isset() alone cannot tell whether the variable is null , because isset() returns false for variables with a value of null , which means that the variable may be undefined or null , and the two cannot be distinguished.

Using is_null() alone will also report an error when the variable is undefined.

So we need to first use isset() to determine whether the variable exists, and then use is_null() to determine whether it is null . This can accurately distinguish between "variable undefined" and "variable null".


Sample code

 <?php
// Assume that the variable to be detected is $var

if (isset($var)) {
    // Variable defined,Determine whether it is null
    if (is_null($var)) {
        echo '$var Defined and the value is null';
    } else {
        echo '$var Defined and the value is not null';
    }
} else {
    echo '$var Undefined';
}
?>

Further optimization example: Encapsulation function

The above logic can be encapsulated into a function for easy reuse:

 <?php
function isDefinedAndNull(&$var): bool {
    return isset($var) && is_null($var);
}

// Example usage
if (isDefinedAndNull($var)) {
    echo '$var Defined and as null';
} else {
    echo '$var Undefined或不为 null';
}
?>

Note that reference pass &$var is used here to prevent the Notice from being triggered when the variable is undefined.


Additional Instructions

  • When the variable is not defined , it will generate a warning.

  • Used in conjunction can avoid unnecessary warnings while accurately distinguishing variable states.

  • isset() not only determines whether it is defined, but also determines whether it is null .

  • If you just want to determine whether the variable exists (not considering whether it is null ), just use isset() .


Summarize

By combining isset() and is_null() , it is possible to accurately judge whether the variable has been defined and the value is null , avoiding misjudgment or warning when used alone. In actual development, such judgments are particularly suitable for processing variables that may not be initialized to ensure the robustness of the code.