: This article mainly introduces the differences between empty and isset. For more information about PHP tutorials, see. I believe that as a newbie, every time we use empty () and isset (), we will have the following questions: What is the difference between them? When do I need to use empty ()? When to use isset ()? Today, I want to tell you the differences between the two and their application scenarios.
1. Summary of differences:
Difference 1: empty () is a function that has all the attributes of a function, has a return value, can use the function return value as a parameter, can use dynamic variables to access;
Isset () is a statement. it is an inherent logical structure of php, such as foreach, for, and continue. it cannot be accessed using dynamic variables or use function return values as parameters; example:
$a = 'empty';$a('err');//TRUEempty(intval('1fa'));//FALSE;
Difference 2: After $ a is defined, whether its value is 0, false, null, ''or other null values, empty () returns true; isset () the return value is true. isset returns false only when verifying undefined variables;
$ A = 0; // '0', false, null, '', '0. 0' and other null values empty ($ a); // trueempty ('aaa'); // falseempty ($ B); // trueisset ($ ); // trueisset ($ B); // false, $ B undefined
2. application scenarios:
A. When determining whether a variable is defined, isset () is available ();
Application: when submitting a form, use the value of isset ($ _ POST ['submit ']) to determine whether to submit the form.
B. When determining whether a variable is null (you do not need to know whether it is assigned a value), empty () is available ();
Application: During form verification, you must verify whether the required value is null.
C. when determining whether a variable already exists and is not empty, use isset () and empty ();
Application: when uploading a file, you must determine that $ _ FILES has been assigned a value and is not empty.
The above introduces the differences between empty and isset, including the content, hope to be helpful to friends who are interested in PHP tutorials.