Home  >  Article  >  php judgment variable

php judgment variable

无忌哥哥
无忌哥哥Original
2018-06-28 09:11:352184browse

3. The difference between is_null(), empty(), isset()

//Special note: These three functions are only suitable for variable judgment, do not use literals directly

* 1. When is_null() returns true?

* 1. The variable has been declared but not initialized, and the default value is null

* 2. The variable is assigned a value of null

* 3. After unset() is destroyed, the variable has a null value

* Summary: If the variable does not exist/has no value assigned/the value is null, then true is returned

$val1;  //已声明,但未赋值
$val2 =  null; //直接用null初始化变量
$val3 = 'php';
unset($val3);  //彻底销毁变量

// Note: Using the ternary operator to output true or false is just for intuition and can be omitted completely

@var_dump(is_null($val1) ? true : false); //true
var_dump(is_null($val2) ? true : false); //true
@var_dump(is_null($val3) ? true : false); //true

// var_dump(is_null('')); //The empty string returns false

* 2. When does empty() return true?

* 1. Empty string, empty array

* 2. null

* 3.0 / '0' / false

* Summary:

* 1. If a variable does not exist, it is either empty or null, which can be determined using is_null() / empty()

* 2. If a variable exists, but its value has no impact on the running result, it is regarded as empty

* 3. Null must be empty, but empty is not necessarily null, because it may be a null value or 0 Or false

*/

$str1 = '';
$str2 = [];
$str3 = '0';
$str4 = 0;
$str5 = null;
$str6 = false;
$str7 = 'peter zhu';
echo &#39;<hr>&#39;;
var_dump(empty($str1) ? true : false);//空字符串
var_dump(empty($str2) ? true : false);//空数组
var_dump(empty($str3) ? true : false);//字符型数字0 
var_dump(empty($str4) ? true : false);//数字0
var_dump(empty($str5) ? true : false);//null值
var_dump(empty($str6) ? true : false);//布尔false
var_dump(empty($str7) ? true : false);//有值且不为空,返回false

* Thinking: To determine whether the user has entered content in the text box, should I use is_null() or empty()?

* Answer: Must Use empty(), not is_null()

* Reason: Because the value of the text box defaults to an empty string, that is, value = '', it has been assigned a value, it is just an empty value

* Therefore, is_null() can only check whether the value is null or not, and empty() must be used

* isset() is the inversion operation of null

* Summary: Variables Already exists, and its value is not null, returns true, otherwise false

$domain = &#39;www.php.cn&#39;;
$name = null;
$job;
echo &#39;<hr>&#39;;
var_dump(isset($var));
var_dump(isset($domain) ? true : false);
var_dump(isset($name) ? true : false);
var_dump(isset($job) ? true : false); //false,未赋值并不报错,与is_null不同

* Summary:

* 1. Variables have two states: declared, undeclared

* 2. Declared variables also have two states: assigned (initialized), unassigned (uninitialized)

* 3. Variables may be assigned value types: null, empty value, non-null Value

* 3.1: null value: is_null()

* 3.2: Empty value: empty()

* 3.3: Null value or non-empty value: isset()

* Basic usage principles:

* 1. For undeclared variables, only isset() can be used to judge

* 2. For declared variables, it is empty Use empty() to judge, and is_null() to judge whether to initialize

echo &#39;<hr color="red">&#39;;

//The first scenario: the variable is not declared

//$a is not declared

var_dump(isset($a) ? true : false);  //不报错,返回false表示未声明

/ /Example: When displaying data in paging, if there is a paging variable page in the current URL, the specified page will be output, otherwise the first page of data will be output by default

$name = isset($_GET[&#39;page&#39;]) ? $_GET[&#39;page&#39;] : 1;

//is_null will give a warning and automatically Execute $a = null, so it will return true

var_dump(is_null($a) ? true : false);

//Because undeclared variables will be automatically initialized to null, and for variables with a value of null, empty() will consider them to be empty, so it will return true

var_dump(empty($a) ? true : false);

//Second scenario: The variable has been declared

$a = &#39;www.php.cn&#39;;
$b = &#39;&#39;;
$c = null;
echo &#39;<hr color="blue">&#39;;
var_dump(isset($a));  //有无判断
var_dump(empty($b));  //非空判断
var_dump(is_null($c)); //null

//Principles for initializing variables:

//1. When the variable type is known

$num = 0; //数值
$userName = &#39;&#39;; //字符串
$isPass = false; //布尔
$books = []; //数组
$student = null; //对象

//2. Not sure what value the variable will eventually hold, it is recommended to initialize it with null

Statement:
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