lundi 14 juin 2021

PHP conditions not working as expected for first and last numbers

The problem with the code below is that for some of the if conditions, it doesn't work for the first and last numbers in the range.

For example if($b >= "0.15" && $b<="0.29"), if the user input was 1.15 it wouldn't print anything even though it's supposed to print 1.25 (same if it was 1.29). while if the user input any other number in that range it would work, like if the user input 2.16 for example, it would print 2.25 which would be correct.

It's the same for some of the others too.

<html>

<body>
  <form method="post">
    Enter First Number:
    <input type="float" name="number1" /><br><br>
    <input type="submit" name="submit" value="Add">
  </form>
  <?php
if(isset($_POST['submit'])) {
  $number1 = $_POST['number1'];
  $whole= intval($number1);
  $b= $number1 - $whole;
  echo "the number is $whole and the decimal is $b";
  echo"<br>";
  if     ($b >= "0.00" && $b<="0.14"){
    echo $whole;}
  else if($b >= "0.15" && $b<="0.29"){
    echo $whole+0.25;}
  else if($b >= "0.30" && $b<="0.44"){
    echo $whole+0.5;}
  else if($b >= "0.45" && $b<="0.59"){
    echo $whole+0.75;}
  else if($b >= "0.60" && $b<="0.74"){
    echo $whole+1.00;}
  else if($b >= "0.74" && $b<="0.89"){
    echo $whole+1.25;}
  else if($b >= "0.90" && $b<="0.99"){
    echo $whole+1.5;}
}
    ?>
</body>

</html>

Aucun commentaire:

Enregistrer un commentaire