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

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

不言
Release: 2023-04-05 15:28:01
forward
2431 people have browsed it

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

?>
Copy after login

Result

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

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
Copy after login

4. Equality relationship between 0, '' and null

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

if(&#39;&#39; == &#39;0&#39; || null == &#39;0&#39; ) 
echo true;
//返回结果 untitled;
Copy after login

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!

Related labels:
php
source:segmentfault.com
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