Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP if statement failing for variable with value "0"

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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']); ?>">
    &nbsp;&nbsp;
    <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:

  1. 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.
  2. converting the $hc to an integer value, assuming the form treats it as a string. But I validated that it returns a number by using an if(is_numeric) which returned my "YES, this is numeric" statement.
  3. several echo statements inside and outside the IF statement, and I never hit the IF statement.
  4. 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.
  5. 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 false itself
  • the integer 0 (zero)
  • the floats 0.0 and -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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading