We believe that as a novice, each time we use empty () and isset (), there will be such doubts: what is the difference between the two? When do I need to use empty ()? When to use Isset ()? Today I'll talk to you about the difference between the two and the application scenario.
1. Summary of differences:
Difference One: Empty () is a function, it has all the properties of the function, there is a return value, you can use the function return value as a parameter, you can use dynamic variable access, etc.;
Isset () is a statement that is an intrinsic logical structure of PHP, such as foreach,for,continue, that cannot be accessed using dynamic variables, and cannot use function return values as arguments;
$a = ' empty '; $a (' err ');//trueempty (Intval (' 1FA '));//false;
Difference two: When a $ A is defined, regardless of its value is 0,false,null, "equal null value, empty () return value is True;isset () The return value is also ture,isset only when validating an undefined variable will return false;
For example:
$a = 0;//' 0 ', false,null, ' ', ' 0.0 ', etc. empty values empty ($a);//trueempty (' AAA ');//falseempty ($b);//trueisset ($a);//trueisset ($b) ;//false, $b undefined
2. Application Scenario:
A. When judging whether a variable is defined, Isset () is available;
Application: The form is submitted with a value of isset ($_post[' submit ') to determine whether to submit the form.
B. When judging whether a variable is empty (you do not need to know whether it is assigned), you can use empty ();
Application: When validating a form, verify that the required value is empty.
C. When judging that a variable already exists and is not empty, you can use Isset () first, then empty ();
Application: When uploading files, it is necessary to determine that $_files has been assigned and is not empty.
The above describes the difference between empty and isset, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.