Why do I need the isset() function in php?

后端 未结 6 1656
终归单人心
终归单人心 2021-01-04 06:27

I am trying to understand the difference between this:

if (isset($_POST[\'Submit\'])) { 
  //do something
}

and

if ($_POST[         


        
相关标签:
6条回答
  • 2021-01-04 07:11

    Because

    $a = array("x" => "0");
    
    if ($a["x"])
      echo "This branch is not executed";
    
    if (isset($a["x"]))
      echo "But this will";
    

    (See also http://hk.php.net/manual/en/function.isset.php and http://hk.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting)

    0 讨论(0)
  • 2021-01-04 07:14

    The code

    
    if($_POST['Submit'])
    {
    //some code
    }
    
    

    will not work in WAMP (works on xampp)
    on WAMP you will have to use

    
    if (isset($_POST['Submit'])) { 
      //do something
    }
    
    

    try it. :)

    0 讨论(0)
  • 2021-01-04 07:15

    You basically want to check if the $_POST[] variable has been submitted at all, regardless of value. If you do not use isset(), certain submissions like submit=0 will fail.

    0 讨论(0)
  • 2021-01-04 07:18

    isset will return TRUE if it exists and is not NULL otherwise it is FALSE.

    0 讨论(0)
  • 2021-01-04 07:22

    if user do not enter a value so $_post[] return NULL that we say in the description of isset:"

    isset will return TRUE if it exists and is not NULL otherwise it is FALSE.,but in here isset return the true "

    0 讨论(0)
  • 2021-01-04 07:23

    In your 2nd example, PHP will issue a notice (on E_NOTICE or stricter) if that key is not set for $_POST.

    Also see this question on Stack Overflow.

    0 讨论(0)
提交回复
热议问题