I am fairly new to PHP and I am creating a basic registration from. I was trying to use isset to check all the variables but without any luck. It seemed all I needed to go was use !empty instead of isset.
Any reason why isset would not work?
if ( (!empty($username)) && (!empty($email)) && (!empty($email2)) && (!empty($pass)) && (!empty($pass2)) )
{
// seems to work
// rest of code to validate
}
isset() returns false if the variable is not set at all or is set to NULL. In addition to those two cases, !empty() will return false if the variable is set but has a value of FALSE, 0, 0.0, or "" (empty string). Thus, for example, you need to be careful about using !empty() when testing for a numeric input where zero is a valid value.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
When you submit a form, say VIA POST, the form sets the value of the form field into the $_POST variable. So if the form field (say, name="username") is left empty, $_POST['username'] will be set, but just to an empty value ("").
When you're validating the form input on the PHP side, using isset($_POST['username']) will make sure that the form field is there, while empty($_POST['username']) will tell you whether or not the user has put anything into that field.
In my personal opinion, it's important to check both when validating form fields as users can alter the form input data using various methods. You can do that by calling each function for each field, or if you're going to be doing that many times, you can create a function.
Bookmarks