Home > Backend Development > PHP Tutorial > Summary of examples of methods to determine whether a variable is empty in PHP

Summary of examples of methods to determine whether a variable is empty in PHP

伊谢尔伦
Release: 2023-03-12 06:24:02
Original
1409 people have browsed it

JudgmentVariable is empty, which is used on many occasions. At the same time, like many novices, I often make some mistakes, so I sorted out some commonly used judgment variables in PHP that determine whether the variable is empty. Method

1. isset function: Determine whether the variable is initialized

Description: It does not determine whether the variable is empty, and can be used to determine whether the array Whether the elements in
have been definedNote: When using isset to determine whether the array elements have been initialized, its efficiency is about 4 times higher than array_key_exists

$a = '';
$a['c'] = '';
if (!isset($a)) echo '$a 未被初始化' . "";
if (!isset($b)) echo '$b 未被初始化' . "";
if (isset($a['c'])) echo '$a 已经被初始化' . "";
// 显示结果为
// $b 未被初始化
// $a 已经被初始化
Copy after login
2. empty function: detect whether the variable is "empty"

Description: Any uninitialized variable, the value is 0 or false or emptyString"" or null Variables, empty arrays, and objects without any attributes
will be judged as empty==true
Note 1: Uninitialized variables can also be detected as "empty" by empty "

Note 2: empty can only detect variables, not statements

$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
3. var == null function: determine whether the variable is "empty"


Note: Variables and empty arrays with a value of 0 or false or empty string "" or null will be judged as null

Note: The significant difference from empty is: when the variable is not initialized, var == null will report an error .

$a = 0;
$b = array();
if ($a == null) echo '$a 为空' . "";
if ($b == null) echo '$b 为空' . "";
if ($c == null) echo '$b 为空' . "";
// 显示结果为
// $a 为空
// $b 为空
// Undefined variable: c
Copy after login
4. is_null function: detect whether the variable is "null"


Description: When the variable is assigned a value of "null", the detection result is true
Note 1: null is not case-sensitive: $a = null; $a = NULL There is no difference
Note 2: The detection result is true only when the value of the variable is "null", 0, empty string, Both false and empty arrays are detected as false

Note 3: When the variable is not initialized, the program will report an error

$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
5. var === null function: detect whether the variable is "null", At the same time, the type of the variable must also be "null"


Note: When the variable is assigned a value of "null" and the type of the variable is also "null", the detection result is true
Note 1 : When it is judged as "null", equal to is_null has the same effect.

Note 2: When the variable is not initialized, the program will report an error

Summary: In PHP, "NULL" and "empty" " are 2 concepts.


isset is mainly used to determine whether a variable has been initialized.
empty can set variables with values ​​of "false", "empty", "0", "NULL", and "uninitialized" Judged as TRUE
is_null Only variables with a value of "NULL" are judged as TRUE
var == null Judged as TRUE are variables with values ​​of "false", "empty", "0", and "NULL"

var === null Only variables with a value of "NULL" are judged as TRUE

So when we judge whether a variable is really "NULL", we mostly use is_null to avoid "false", Interference equivalent to "0". ###

The above is the detailed content of Summary of examples of methods to determine whether a variable is empty in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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