Home > Backend Development > PHP Tutorial > The difference between empty() and isset() in PHP_PHP Tutorial

The difference between empty() and isset() in PHP_PHP Tutorial

WBOY
Release: 2016-07-12 09:07:36
Original
1082 people have browsed it

The difference between empty() and isset() in PHP

1. empty function
Purpose: Check whether the variable is empty
Judgment: If var is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any properties will be considered empty, and TRUE is returned if var is empty. Source manual: http://php.net/manual/zh/function.empty.php
Note: empty() only detects variables, detecting anything that is not a variable will result in a parsing error.
$name=0;
$name='';
$name=null;
$name="0";
$name;
if(empty($name))
{
echo "ok"; //The above five ways of writing empty($name) are all true
}
if(empty($na)) //The result is true because the variable $na is undefined
2. isset function
Purpose: Check whether the variable is set
Judgment: Check whether the variable is set and not NULL. If a variable has been freed using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("
isset function is used to detect whether this variable has been set. In the following two cases, isset will be considered false
①$id; No value is given
② Or there is no definition at all.
Therefore, we recommend using isset to determine whether a form is submitted with data if(isset($_GET/$_POST['Variable']){echo $_GET['Variable']}else{ echo 'No Pass data'}
Example:
$a1 = null;
$a2 = false;
$a3 = 0;
$a4 = '';
$a5 = '0';
$a6 = 'null';
$a7 = array();
$a8 = array(array());
echo empty($a1) ? ‘true’ : ‘false’;//output true
echo empty($a2) ? ‘true’ : ‘false’;//output true
echo empty($a3) ? ‘true’ : ‘false’;//output true
echo empty($a4) ? ‘true’ : ‘false’;//output true
echo empty($a5) ? ‘true’ : ‘false’;//output true
echo empty($a6) ? ‘true’ : ‘false’;//output false
echo empty($a7) ? ‘true’ : ‘false’;//output true
echo empty($a8) ? ‘true’ : ‘false’;//output false
echo ‘
’;
echo isset($a1) ? ‘true’ : ‘false’;//output false
echo isset($a2) ? ‘true’ : ‘false’;//Output true
echo isset($a3) ? ‘true’ : ‘false’;//Output true
echo isset($a4) ? ‘true’ : ‘false’;//output true
echo isset($a5) ? ‘true’ : ‘false’;//output true
echo isset($a6) ? ‘true’ : ‘false’;//Output true
echo isset($a7) ? ‘true’ : ‘false’;//output true
echo isset($a8) ? ‘true’ : ‘false’;//output true
1. The isset value is judged as false when the variable is not assigned a value or is assigned a value of NULL, and the rest are true;
2. There are a lot of things to pay attention to in empty, and you have to make a judgment based on the definition of the function.

http://www.bkjia.com/PHPjc/1060168.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1060168.htmlTechArticleThe difference between empty() and isset() in PHP 1. Purpose of empty function: Detect whether the variable is empty and judge whether it is empty: If var is a non-empty or non-zero value, empty() returns FALSE. In other words, 0, 0,...
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