Current Location: Home> Latest Articles> The difference and comparison between is_null and empty functions

The difference and comparison between is_null and empty functions

gitbox 2025-05-31

In PHP programming, is_null and empty are two commonly used functions that are used to judge the state of variables, but their functions are obviously different from those of use. This article will compare these two functions in detail to help you understand their differences and use them reasonably in actual development.


1. Function definition and basic functions

  • is_null($var)
    Determine whether the variable is NULL . If the value of the variable is strictly NULL , return true , otherwise false .

  • empty($var)
    Determine whether the variable is empty. The "empty" here is relatively broad. In addition to NULL , it also includes false , empty string "" , 0 , "0" , empty array [] and other situations. Return true if the value of the variable is considered "empty", otherwise return false .


2. Detailed explanation of the specific differences

Comparison items is_null empty
Judgment content Only determine whether it is NULL Determine whether the variable is empty ( NULL , false , 0 , empty string, empty array, etc.)
Is the variable declared? The variable must be declared, otherwise an error will be reported or a message will be reported. No error will be reported when the variable is not declared, and it will be returned directly .
Applicable scenarios It is necessary to strictly judge whether the variable is NULL Determine whether the variable is "meaningless" or "null"
Results Example is_null(0) returns false empty(0) returns true

3. Code sample comparison

 <?php
$var1 = null;
$var2 = 0;
$var3 = "";
$var4 = false;
$var5 = [];

var_dump(is_null($var1)); // true
var_dump(empty($var1));   // true

var_dump(is_null($var2)); // false
var_dump(empty($var2));   // true

var_dump(is_null($var3)); // false
var_dump(empty($var3));   // true

var_dump(is_null($var4)); // false
var_dump(empty($var4));   // true

var_dump(is_null($var5)); // false
var_dump(empty($var5));   // true

// The case where the variable is not defined
var_dump(empty($undefined_var)); // true
// var_dump(is_null($undefined_var)); // Will report an error:Undefined variables
?>

4. Precautions and suggestions

  • Handling of undefined variables <br> When using empty to check that undefined variables will not be reported, it is suitable to make "null value" judgments when the variable is not declared. is_null requires that the variable must be declared, otherwise it will cause an error.

  • Logical Requirements Selection <br> If you need to strictly judge whether the variable is assigned to NULL , is_null should be used. If you determine whether the variable is "equivalent to empty", it is more appropriate to use empty .

  • The difference from isset
    Isset is used to determine whether the variable has been set and not NULL . It can be used together with these two functions to more accurately control the variable state.


5. Practical application examples

Suppose you get data from an API and determine whether the data is valid:

 <?php
$response = file_get_contents("https://gitbox.net/api/data");

$data = json_decode($response, true);

if (is_null($data['value'])) {
    echo "The data is NULL,Need to request again";
}

if (empty($data['value'])) {
    echo "The data is空,may be 0、Empty string or not set";
}
?>

In the above code example, is_null is used to determine the exact NULL , while empty covers a wider range of "invalid" or "empty" cases.