Looking for clarity on my PHP code. I created a Registration form and checked whether the user entered all data on the form:
if($fn && $ln && $un && $sx && $em && $pw && $hc) {
// then do this
}
But it skips this check completely even if all data is entered by the user.
echo 'FN is: '.$fn.'!<br>'; // successfully returns the user first name
echo 'LN is: '.$ln.'!<br>'; // successfully returns the user last name
echo 'UN is: '.$un.'!<br>'; // successfully returns the user username
echo 'SX is: '.$sx.'!<br>'; // successfully returns the user gender/sex
echo 'EM is: '.$em.'!<br>'; // successfully returns the user email
echo 'PW is: '.$pw.'!<br>'; // successfully returns the user password (hashed)
echo 'HC is: '.$hc.'!<br>'; // successfully returns the user handicap as integer
My only concern is the validation of $hc and not the other variables. My HTML form has this:
<p>
<strong>Handicap:</strong>
<input type="numberR" name="handicap" min="0" max="50" step="0.1" value="<?php if(isset($trimmed['handicap'])) echo ($trimmed['handicap']); ?>">
<small>If none, enter zero.</small>
</p>
And here are my various attempts for storing the variables:
// Check if a numerical 0-99 handicap was entered on the form.
if (is_numeric($trimmed['handicap'])) {
$hc = mysqli_real_escape_string($dbc, $trimmed['handicap']);
}
if(preg_match('/^[0-9]{1,2}$/', $trimmed['handicap'])) {
$nhc = (int)mysqli_real_escape_string($dbc, $trimmed['handicap']);
}
These are the solutions I tried to validate whether my code was hitting/getting to the IF check:
- changing the input type back to "number", but I really needed a customized box here which is why I’m using "numberR". My ‘number’ input types are being reserved for another purpose.
- converting the
$hcto an integer value, assuming the form treats it as a string. But I validated that it returns a number by using anif(is_numeric)which returned my "YES, this is numeric" statement. - several echo statements inside and outside the IF statement, and I never hit the IF statement.
- The check DOES successfully pass the IF statement if the user enters a 1 value. But users who are new MUST be able to enter zero.
- The database isn’t the issue. The connection is fine, and if the user enters the value of 1, it posts to the DB in the appropriate column/table.
The thing that eventually worked was to remove $hc from the IF statement entirely. But I’m perplexed why that actually works. Perhaps it’s something about a string/number conversion, or some null logic, but I’m only in my first year of PHP, SQL, CSS, and HTML coding, and I’m trying to absorb as much as possible. And though it’s not critical for my little project, ideally that check should validate that the $hc was entered.
Can anyone point to what I might be missing? Is more of my code needed for an evaluation? I’m really just looking for understanding, and not actual code. Forgive my newbieness, and thanks in advance.
>Solution :
This is because your input of 0 is a falsy value, which means when it is evaluated as an expression within an if statement, it will be false.
When converting to bool, the following values are considered false:
- the boolean
falseitself - the integer
0(zero) - the floats
0.0and-0.0(zero) - the empty string
"", and the string"0" - an array with zero elements the unit type
NULL(including unset variables)
To determine if a variable has a value, you can use isset(), this will evaluate to true regardless if the variable contains a falsy value:
if ($fn && $ln && $un && $sx && $em && $pw && isset($hc)) {
// then do this
}
Reference: PHP Booleans: Converting to boolean