0
votes

I have a form with username and password and the value gets posted to check.php when I have no input in username and password,(left blank), isset($_POST["username"]) and isset($_POST["password"]) returns true, which it should not be as I did not set anything. But empty($_POST["username"] returns true as is expected. can someone explain this behaviour of isset.

Thanks

3
kunststube.net/isset will explain it all for you.vascowhite

3 Answers

2
votes

If you submit form elements...

<input name='username'...

...but they're empty, it will fill the $_POST array with empty values by default. So, the variables are set, but they're empty ("").

What I usually do in this case is something like

if (isset($_POST['username'] && $_POST['username']) { ... }

In that case, it will check for if it's set (that is, is the form submitted) and if the values aren't empty. If they're empty ("") PHP will interpret as false and the condition will not be met.

1
votes

That may be because your $_POST['username'] is set and may be empty, so to overcome check like this

if ((isset($_POST["username"]) && ($_POST["username"]!=""))
0
votes

isset — Determine if a variable is set and is not NULL. If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered. refer :- http://php.net/manual/en/function.isset.php