The function of Isset () and empty () is very similar, can only say is very similar, because they still have different points, and their differences are quite obvious;
The explanations for these two in the Official Handbook are as follows:
bool
empty ( mixed
$var
) Determine if a variable is considered empty. When a variable does not exist, or its value is equal to
FALSE
, then it will be considered non-existent. If the variable does not exist,
Empty () does not produce a warning. bool
isset ( mixed
$var
[, mixed
$...
]) Detects if the variable is set, and is not
NULL
。 I often use these two functions when judging whether a form is being submitted, but sometimes it can be confusing. For example, before I perform various operations, I have to determine whether there is a form submission? Do you want to submit it in the specified way (get or post)? What is the commit action (delete, display, insert)? At this point I have to use empty or isset, said often confused, because I was the first to use this, such as the following code:
<form action= "" method= "post" > <input type= "text" name= "Test" > <input type= "Submit" Name= " Submit "value=" Delete "> <input type=" Submit "name=" Submit "value=" Update "> </form> < PHP if (isset ($_post)) { echo "received request"; } else{ echo "not received request"; } ? >
In fact, this is wrong, because $_post is a global array, even if the $_post array is empty, that $_post also exists, regardless of whether there is a POST request, the code will output "received request." You might think of using isset to determine if the Name property of the submit is empty, such as the following code:
<form action= "" method= "post" > <input type= "text" name= "Test" > <input type= "Submit" Name= " Submit "value=" Delete "> <input type=" Submit "name=" Submit "value=" Update "> </form> <?php if (isset ($_post[' submit ')) && $_post[' submit ']== ' delete ') { echo ' received deletion request '; } elseif (Isset ($_ post[' Submit ']) && $_post[' Submit ']== ' update ' { echo ' received update request '; ' Else ' Echo ' not received request '; ? >
It can also be implemented in the following way:
<meta charset= ' utf-8 ' > <form action= "" method= "post" > <input type= "text" name= "Test" > <input type= "Submit" Name= "submit" value= "Delete" > <input type= "Submit" name= "Submit" value= "Update" > </form> <? PHP if (! empty ($_post)) {$action =$_post[' submit ']; switch ($action) {case "delete": echo "delete"; Update ": echo" Update "; Default:echo "unlawful Submission"; }} else {echo "not received request";}?>
PHP isset () and Empty ()