How to detect if a variable is empty in php

silencement
Release: 2023-02-24 22:32:01
Original
2332 people have browsed it

How to detect if a variable is empty in php

In program development, we encounter a lot of judgments. Sometimes when we need to judge whether a variable is empty, isset(), empty(), and is_null() are always Confused, let’s introduce their differences below

1. isset, determines whether the variable has been initialized

isset(), determines whether the variable has been set, it will not determine whether the variable is empty. And can be used to determine whether the elements in the array have been defined

Code description:

<?php
$a = &#39;&#39;;
$a[&#39;c&#39;] = &#39;&#39;;
if (!isset($a)) echo &#39;$a 未被初始化&#39; . "";
if (!isset($b)) echo &#39;$b 未被初始化&#39; . "";
if (isset($a[&#39;c&#39;])) echo &#39;$a 已经被初始化&#39; . "";
// 显示结果为
// $b 未被初始化
// $a 已经被初始化
?>
Copy after login

2. empty, detect whether the variable is "empty"

empty(), any An uninitialized variable, a variable with a value of 0 or false or an empty string "" or null, an empty array, or an object 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

Code description:

<?php
$a = 0;
$b = &#39;&#39;;
$c = array();
if (empty($a)) echo &#39;$a 为空&#39; . "";
if (empty($b)) echo &#39;$b 为空&#39; . "";
if (empty($c)) echo &#39;$c 为空&#39; . "";
if (empty($d)) echo &#39;$d 为空&#39; . "";
?>
Copy after login

3. $a == null, determine whether the variable is "empty"

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

Note: The obvious difference from empty is that if the variable $a is not initialized, $a == null will report an error.

Code description:

<?php
$a = 0;
$b = array();
if ($a == null) echo &#39;$a 为空&#39; . "";
if ($b == null) echo &#39;$b 为空&#39; . "";
if ($c == null) echo &#39;$b 为空&#39; . "";
// 显示结果为
// $a 为空
// $b 为空
// Undefined variable: c
?>
Copy after login

4. is_null, detects whether the variable is "null"

is_null(), 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 makes no difference

Note 2: The detection result is only when the value of the variable is "null" is true, 0, empty string, false, and empty array are all detected as false

Note 3: When the variable is not initialized, an error will also be reported

Code description:

<?php
$a = null;
$b = false;
if (is_null($a)) echo &#39;$a 为NULL&#39; . "";
if (is_null($b)) echo &#39;$b 为NULL&#39; . "";
if (is_null($c)) echo &#39;$c 为NULL&#39; . "";
// 显示结果为
// $a 为NULL
// Undefined variable: c
?>
Copy after login

5. $a === null, detect whether the variable is "null", and the type of the variable must also be "null"

Description: When a variable is assigned a value of "null", the type of the variable must also be "null" When it is also "null", the detection result is true

Note 1: When it is judged to be "null", it has the same effect as is_null

Note 2: When the variable is not initialized, the program also An error will be reported

Summary:

In PHP, "NULL" and "empty" are two 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" Judge as TRUE

is_null Only judge the variable whose value is "NULL" as TRUE

$a == null Judge as "false", "empty", "0", "NULL" " variables are all judged to be TRUE

$a === null Only variables with a value of "NULL" are judged to be TRUE

, so when we judge whether a variable is really "NULL" , mostly use is_null to avoid interference from values ​​such as "false" and "0".

The above is the detailed content of How to detect if 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!