Home  >  Article  >  Backend Development  >  What is the difference between isset() and empty() functions in PHP?

What is the difference between isset() and empty() functions in PHP?

不言
不言forward
2019-03-13 13:59:042337browse

What this article brings to you is about the difference between isset() and empty() functions in PHP? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Many people only think about advanced technology, but don’t even have enough basic knowledge! It's ridiculous to be asked a simple question to the point of panic! right! I'm talking about myself! Next, I will continue to make some simple knowledge summaries.

1. Definition of isset()

Detect whether the variable has been set and is not NULL

<?php

$var = &#39;&#39;;

// 结果为 TRUE,所以后边的文本将被打印出来。
if (isset($var)) {
    echo "This var is set so I will print.";
}

// 在后边的例子中,我们将使用 var_dump 输出 isset() 的返回值。
// the return value of isset().

$a = "test";
$b = "anothertest";

var_dump(isset($a));      // TRUE
var_dump(isset($a, $b)); // TRUE

unset ($a);

var_dump(isset($a));     // FALSE
var_dump(isset($a, $b)); // FALSE

$foo = NULL;
var_dump(isset($foo));   // FALSE

?>

Result

This var is set so I will print.bool(true)
bool(true)
bool(false)
bool(false)
bool(false)

2. Definition of empty()

==Check whether a variable is empty==

The following things are considered empty:

"" (empty string)

0 (0 as an integer)

0.0 (0 as a floating point number)

"0" (as a character 0 of the string)

NULL

FALSE

array() (an empty array)

$var; (one declared but with no value Variable)

3. Comparison of isset() and empty()

$var = 0;
if (empty($var)) {
    echo 1;
}//1
if (isset($var)) {
    echo 2;
}//1

4. Equality relationship between 0, '' and null

if('' == null && '' == 0 && null ==  0 && 0 =='0') 
echo true;
//返回结果  1;

if('' == '0' || null == '0' ) 
echo true;
//返回结果 untitled;

Conclusion: ==' ', null, 0, '0' are equal to each other; '', null is not equal to '0'==

The above is the detailed content of What is the difference between isset() and empty() functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete