Detailed example tests of PHP empty of isset of is_null of

  • 2020-06-07 04:05:02
  • OfStack

The use of empty(), isset(), and is_null() functions of PHP has been much discussed, and much of the information is not necessarily clear. Here again repeat 1, but not from the concept to say, direct use of program examples to speak, should memory will be more profound.

The types of tests are as follows:


<?php

$a;
$b = false;
$c = '';
$d = 0;
$e = null;
$f = array();

?>
empty()

The first is 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));

?>

The program output is:


bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

As you can see from the code, empty() prints true as long as the data type is null or false.

isset()

Consider the output of isset:


var_dump(isset($a));
var_dump(isset($b));
var_dump(isset($c));
var_dump(isset($d));
var_dump(isset($e));
var_dump(isset($f));

//  The output 
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)

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

is_null()

Finally, 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));

//  The output 
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)

is_null literally.

Thus, empty() can be used to determine whether all data types are null or false, while is_null, like isset, can only be used to determine whether it is NULL and undefined.


Related articles: