Home  >  Article  >  Backend Development  >  Detailed examples of the differences between empty, isset and isnull in php

Detailed examples of the differences between empty, isset and isnull in php

王林
王林forward
2020-01-20 16:26:482210browse

Detailed examples of the differences between empty, isset and isnull in php

empty

If the variable is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var, undefined; and objects without any attributes will be considered empty, and TRUE will be returned if var is empty. .

Code example:

$a = 0;
$b = '';
$c = array();
if (empty($a)) echo '$a 为空' . "";
if (empty($b)) echo '$b 为空' . "";
if (empty($c)) echo '$c 为空' . "";
if (empty($d)) echo '$d 为空' . "";
// 以上输出皆为空

isset (Online learning video tutorial sharing: php video tutorial)

If the variable exists ( non-NULL), returns TRUE, otherwise returns FALSE (including undefined). The variable value is set to: null, and the return value is also false; after unsetting a variable, the variable is canceled. Note that isset handles NULL value variables specially.

Code example:

$a = '';
$a['c'] = '';
if (!isset($a)) echo '$a 未被初始化' . "";
if (!isset($b)) echo '$b 未被初始化' . "";
if (isset($a['c'])) echo '$a 已经被初始化' . "";
// 显示结果为
// $b 未被初始化
// $a 已经被初始化

is_null

Detect whether the incoming value [value, variable, expression] is null, only one variable is defined , and its value is null, it returns TRUE. Others return FALSE [An error will occur after undefined variables are passed in! 】

Code example:

$a = null;
$b = false;
if (is_null($a)) echo '$a 为NULL' . "";
if (is_null($b)) echo '$b 为NULL' . "";
if (is_null($c)) echo '$c 为NULL' . "";
// 显示结果为
// $a 为NULL
// Undefined variable: c

Recommended related articles and tutorials: php tutorial

The above is the detailed content of Detailed examples of the differences between empty, isset and isnull in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete