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

zeckdude picture zeckdude · Mar 17, 2010 · Viewed 16.1k times · Source

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?

Answer

kennytm picture kennytm · Mar 17, 2010

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)