Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 732
zeckdude Avatar asked Mar 17 '10 06:03

zeckdude


People also ask

Why do we use isset function in PHP?

Definition and Usage The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

What can I use instead of isset in PHP?

The equivalent of isset($var) for a function return value is func() === null . isset basically does a !== null comparison, without throwing an error if the tested variable does not exist.

Why we use if isset ($_ POST submit ))?

Code explained isset( $_POST['submit'] ) : This line checks if the form is submitted using the isset() function, but works only if the form input type submit has a name attribute (name=”submit”).

What's the difference between isset () and Array_key_exists ()?

Difference between isset() and array_key_exists() Function: The main difference between isset() and array_key_exists() function is that the array_key_exists() function will definitely tells if a key exists in an array, whereas isset() will only return true if the key/variable exists and is not null.


1 Answers

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)

like image 110
kennytm Avatar answered Sep 28 '22 23:09

kennytm