PHP isset () and Empty ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.