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

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

王林
Release: 2023-04-08 09:06:01
forward
2267 people have browsed it

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 为空' . "";
// 以上输出皆为空
Copy after login

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 已经被初始化
Copy after login

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
Copy after login

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!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template