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

断了今生、忘了曾经 提交于 2019-11-30 16:50:14

问题


I am trying to understand the difference between this:

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

and

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

It seems to me that if the $_POST['Submit'] variable is true, then it is set. Why would I need the isset() function in this case?


回答1:


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)




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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. :)




回答6:


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 "



来源:https://stackoverflow.com/questions/2460290/why-do-i-need-the-isset-function-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!