Php 简明教程

PHP is_null() Function

PHP 将 NULL 定义为其一种特殊数据类型。它表示某个变量尚未分配任何特定数据类型的任何值。它是 PHP 中的一个内置常量,用于指示任何对象或值的故意缺失。可以明确地将变量分配为 NULL,或使用 unset() 函数将值设置为 null。

PHP defines NULL as one of its special data types. It indicates that a certain variable has not been assigned a value any specific data type. It is a built-in constant in PHP and is used to indicate the intentional absence of any object or value. A variable can be explicitly assigned NULL or its value been set to null by using the unset() function.

The is_null() Function

PHP 提供了一个布尔函数 is_null() 来检查变量是否确实为 NULL 类型。

PHP provides a Boolean function is_null() to check if a variable is indeed of NULL type.

is_null(mixed $value): bool

Example 1

如果任何变量明确地分配为 NULL,则显然 is_null() 函数返回 true

If any variable is explicitly assigned NULL, obviously the is_null() function returns true.

<?php
   $x = NULL;
   echo "Variable \$x is null? ";
   var_dump(is_null($x));
?>

它将生成以下 output

It will produce the following output

Variable $x is null? bool(true)

Example 2

如果 unset 具有特定值的变量,则 is_null() 函数也返回 true,但带有警告

If a variable with a certain value is unset, then too the is_null() function returns true, but with a warning

<?php
   $x = "Hello";
   unset($x);
   echo "Variable \$x is null?\n";
   var_dump(is_null($x));
?>

它将生成以下 output

It will produce the following output

Variable $x is null?
bool(true)

PHP Warning:  Undefined variable $x in /home/cg/root/89262/main.php on line 5

Example 3

同样,如果您仅声明一个变量而不为其分配任何值,则 is_null() 函数会返回 true 并带有警告 -

Similarly, if you just declare a variable, without assigning any value to it, the is_null() function returns true with a warning −

<?php
   $y;
   echo "Variable \$y is null?\n";
   var_dump(is_null($y));
?>

它将生成以下 output

It will produce the following output

Variable $y is null?
bool(true)
Warning: Undefined variable $y in hello.php on line 9

Example 4

您还可以使用相等操作符 (==) 检查变量是否为 NULL。

You can also use the equality operator (==) to check if a variable is NULL.

<?php
   $x = NULL;
   if ($x === NULL) {
      echo '$x is NULL';
   } else {
      echo '$x is not NULL';
   }
?>

它将生成以下 output

It will produce the following output

$x is NULL

Example 5

空字符串 "" 不认为等同于 NULL。因此,is_null() 函数以及 =="" 操作符返回 false 。请看以下示例 −

A null string "" is not considered equal to NULL. Hence, the is_null() function as well as the "==" operator return false. Take a look at the following example −

<?php
   $y = "";
   if ($y === NULL) {
      echo '$y is NULL';
   } else {
      echo '$y is not NULL';
   }
   echo "$y is null?\n";
   var_dump(is_null($y));
?>

它将生成以下 output

It will produce the following output

$y is not NULL is null?
bool(false)

PHP 中其他两个与 is_null() 函数相关的函数是 isset() 函数和 empty() 函数。

Two other functions in PHP that are relevant to is_null() function are the isset() function and the empty() function.

The isset() Function

isset() 函数确定变量是否已声明且与 NULL 不同。

The isset() function determines if a variable is declared and is different than NULL.

isset(mixed $var, mixed ...$vars): bool

Example

分配了 NULL 的变量被认为是未设置的。

A variable that is assigned NULL is considered as unset.

<?php
   $x = NULL;
   echo '$x is set? ';
   var_dump(isset($x));
?>

它将生成以下 output

It will produce the following output

$x is set? bool(false)

请注意,空字符 ("\0") 不等同于 PHP null 常量。

Note that a null character ("\0") is not equivalent to the PHP null constant.

The empty() Function

empty() 函数检查一个变量是否被认为是空的。如果变量不存在或其值为 NULL,则该变量被认为是空的。如果变量不存在,empty() 不会生成警告。

The empty() function checks if a variable is considered to be empty. A variable is considered empty if it does not exist or if its value is NULL. empty() does not generate a warning if the variable does not exist.

Example 1

请看以下示例:

Take a look at the following example −

<?php
   $x = NULL;
   echo '$x is empty? ';
   var_dump(empty($x));
   $y;
   echo '$y is empty? ';
   var_dump(empty($y));
?>

它将生成以下 output

It will produce the following output

$x is empty? bool(true)
$y is empty? bool(true)

Example 2

如果变量设置为 "0"、NULL 或根本未设置,empty() 函数返回 true

The empty() function returns true if a variable is set to "0", NULL, or is not set at all.

<?php
   $var = 0;
   if (empty($var)) {
      echo '$var is either 0, empty, or not set at all';
   }
?>

它将生成以下 output

It will produce the following output

$var is either 0, empty, or not set at all