Home > Backend Development > PHP Tutorial > The difference between empty and isset

The difference between empty and isset

WBOY
Release: 2016-07-29 08:57:46
Original
3449 people have browsed it

I believe that as novices, we will have this doubt every time we use empty() and isset(): What is the difference between the two? When do you need to use empty()? When to use isset()? Today I will tell you about the difference between the two and their application scenarios.

1. Summary of differences:

Difference 1: empty() is a function, it has all the attributes of a function, has a return value, can use the function return value as a parameter, can use dynamic variable access, etc.;

isset() is Statement, which is an inherent logical structure of PHP, such as foreach, for, continue, etc., cannot be accessed using dynamic variables, and cannot use function return values ​​as parameters; examples are as follows:

$a = 'empty';
$a('err');//TRUE
empty(intval('1fa'));//FALSE;
Copy after login

Difference 2: When $a is defined, Regardless of whether its value is 0, false, null, '' and other empty values, the return value of empty() is true; the return value of isset() is also TRUE, isset will only return false when verifying undefined variables. ;

For example:

$a = 0;//'0',false,null,'','0.0'等空值
empty($a);//true
empty('aaa');//false
empty($b);//true
isset($a);//true
isset($b);//false,$b未定义
Copy after login

2. Application scenario:

a. When judging whether a variable is defined, isset() can be used;

Application: use the value of isset($_POST['submit']) when submitting the form to determine whether to submit the form.

b. When judging whether a variable is empty (you don’t need to know whether it is assigned a value), you can use empty();

Application: When performing form verification, you need to verify whether the value of the required item is empty.

c. When judging that a variable already exists and is not empty, you can use isset() first, and then empty();

Application: When uploading files, you need to judge that $_FILES has been assigned a value and is not empty.

The above has introduced the difference between empty and isset, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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