Home > Backend Development > PHP Tutorial > The difference between php empty(), isset() and is_null()

The difference between php empty(), isset() and is_null()

伊谢尔伦
Release: 2023-03-02 21:00:01
Original
1000 people have browsed it

There has been a lot of discussion about the usage of PHP's empty(), isset() and is_null() functions, and a lot of information may not be very clear. I repeat it here again, but instead of speaking from concepts, it should be more profound to memorize it by directly using program examples.

The types of tests are as follows:

<?php
 
$a;
$b = false;
$c = &#39;&#39;;
$d = 0;
$e = null;
$f = array();
 
?>
Copy after login

empty()

First is the var_dump output of empty:

<?php
 
var_dump(empty($a));
var_dump(empty($b));
var_dump(empty($c));
var_dump(empty($d));
var_dump(empty($e));
var_dump(empty($f));
 
?>
Copy after login

The program output is:

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Copy after login

As can be seen from the code, as long as the data type is empty or false , empty() will output true.

isset()

Look at the output of isset again:

var_dump(isset($a));
var_dump(isset($b));
var_dump(isset($c));
var_dump(isset($d));
var_dump(isset($e));
var_dump(isset($f));
 
// 输出
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
Copy after login

It can be seen that isset() can only be used to determine whether it is NULL and undefined.

is_null()

Finally is the output of is_null:

var_dump(is_null($a));
var_dump(is_null($b));
var_dump(is_null($c));
var_dump(is_null($d));
var_dump(is_null($e));
var_dump(is_null($f));
 
// 输出
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
Copy after login

is_null literally means it.

It can be seen that empty() can be used to determine whether all data types are empty or false, while is_null is basically the same as isset and can only be used to determine whether it is NULL and undefined


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