0

That piece of code does its job as long i want to know the bmi. But it should also check if the formula is filled with numbers and the else statement should kick in if that is not the case. although it seems to kick in it always points out error messages like:

((this is not a duplicate of another question i was linked to. There it talks about another topic which is about new error messages but this is about how i have to write my if-else statement so the check for numeric pulls out the else statement.))

Warning: A non-numeric value encountered in C:\xampp\htdocs\workspace\4.5\Aufgabe19.php on line 15

Warning: A non-numeric value encountered in C:\xampp\htdocs\workspace\4.5\Aufgabe19.php on line 15

Warning: A non-numeric value encountered in C:\xampp\htdocs\workspace\4.5\Aufgabe19.php on line 16

which it should not do. It should just say: bitte geben sie zahlen ein, if the formular is filled with other things.

        <?php
    // Variablen deklararien und Werte zuweisen.
    $gewicht = $_POST['gewicht'];
    $koerpergroesse = $_POST['koerpergroesse'];
$bmi = 0;
$dezimal = 0;

        $dezimal = ($koerpergroesse /100) * ($koerpergroesse / 100);
  $bmi = $gewicht / $dezimal;


if (is_numeric($gewicht) && is_numeric($koerpergroesse)) {
    if ($bmi < 18.5) {

     echo "Ihr BMI beträgt: " . round($bmi,2) . "<br>";
     echo "Sie haben Untergewicht.";
    }

  if ($bmi >= 18.5 && $bmi < 25) {

     echo "Ihr BMI beträgt: " . round($bmi,2) . "<br>";
     echo "Sie haben Normalgewicht.";
  }

  if ($bmi >= 25 && $bmi < 30) {

     echo "Ihr BMI beträgt: " . round($bmi,2) . "<br>";
     echo "Sie haben leichtes Übergewicht.";
  }

  if ($bmi >= 30 && $bmi < 40) {

     echo "Ihr BMI beträgt: " . round($bmi,2) . "<br>";
     echo "Sie haben starkes Übergewicht.";
  }

  if ($bmi >= 40) {

     echo "Ihr BMI beträgt: " . round($bmi,2) . "<br>";
     echo "Sie haben lebensbedrohliches Übergewicht.";
  }
}
else {

echo "Bitte geben sie Zahlen ein.";
}

  ?>
thor
  • 21,418
  • 31
  • 87
  • 173
  • 3
    Possible duplicate of [Warning: A non-numeric value encountered](http://stackoverflow.com/questions/42044127/warning-a-non-numeric-value-encountered) – Masivuye Cokile Mar 28 '17 at 15:12

2 Answers2

0

I can not get your question clearly..But what I think that you want to alert user to enter value and it should be numeric.....instead of error shown as described by you in question....May be I am wrong in judging your question...

Try like this.

  $error = false; // this should be before checking if post submitted...

  if ( ((!isset( $gewicht)) || (empty( $gewicht)) )  && ( (!isset( $koerpergroesse )) || (empty( $koerpergroesse ))) ){
   $error=true;
   echo "Please fill up values";
   }

  if ( (!is_numeric($gewicht)) && (!is_numeric($koerpergroesse)) ){
    $error=true;
    echo "Value must be numeric";
   }

  if (!$error) {
     if ($bmi < 18.5) {.......
  • sorry my englisch is not good anymore. but i think Alex Howansky answered the question i had. Still your solution is interesting, too. I will try both of them – flightofthepenguin Mar 28 '17 at 15:55
0

You're using the non-numeric values in calculations before you test them to see if they're numeric. The POST values contain invalid strings. Then you use immediately them in a calculation, before you do the check to see if they're numeric:

$gewicht = $_POST['gewicht'];
$koerpergroesse = $_POST['koerpergroesse'];
$dezimal = ($koerpergroesse /100) * ($koerpergroesse / 100);
$bmi = $gewicht / $dezimal;
if (is_numeric($gewicht) && is_numeric($koerpergroesse)) {

You should put the calculations inside the if statement:

$gewicht = $_POST['gewicht'];
$koerpergroesse = $_POST['koerpergroesse'];
if (is_numeric($gewicht) && is_numeric($koerpergroesse)) {
    $dezimal = ($koerpergroesse /100) * ($koerpergroesse / 100);
    $bmi = $gewicht / $dezimal;
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • yes, that is absolutely true. Maybe next time is should just make a pause before i check my coding. After you wrote it, i did not know how i overlooked it. – flightofthepenguin Mar 28 '17 at 16:00