PHP Empty


PHP empty() is used to detect whether a variable is empty, if the detected variable is empty, return TRUE, otherwise return FALSE.

grammar:

bool empty(mixed var)

example:

$var = " ";
if (empty($var)) {
    echo 'variable $var is empty';
} Else {
    echo 'variable $var is not empty';
}

Run the example output:

The variable $var is empty
Note
  • empty() can only be used to detect variables, such as this usage is illegal: empty(addslashes($var)).
  • empty() is a language construct, not a function, so it can not be called by a variable function.
Tip

The empty() function returns TRUE if:

// Variable has no value
$var = " ";
// Variable value is 0 or " 0 "
$var = 0;
$var = " 0 ";
// An empty array
$var = array();
// The variable is set to null
$var = null;
// The variable is set to false
$var = false;
// unset() function to release the variable
unset($var);
// The class variable is declared by the var keyword, but has not been set
var $var;

Comparison of empty() and isset()

empty() isset() (bool)
$var = "" TRUE TRUE FALSE
$var = null TRUE FALSE FALSE
$var = FALSE TRUE TRUE FALSE
var $var TRUE FALSE FALSE
$var = array() TRUE TRUE FALSE
$var = 0 TRUE TRUE FALSE
$var = "0" TRUE TRUE FALSE

empty() is more stringent than isset() on the inspection variables.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook