Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Fatal error: Unsupported operand types *(issues with basic math)*

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #163470
    Attila Hajzer
    Participant
    <?php
    // Attila Hajzer
    $weight = $_POST["txtWeight"];
    $height = $_POST["txtHeight"];
    $unit = $_POST["optUnit"];
    $message = "";
    
    if($unit == "metric")
    {
        $bmiCalc = $weight/($height*$height); #This Line
        $message = "Metric BMI: $bmiCalc";
    }
    else
    {
        $bmiCalc = ($weight * 703)/($height * $height);
        $message = "Imperial BMI: $bmiCalc";
    }
    
    ?>
    

    What am I doing wrong ?

    #163526
    Alen
    Participant

    Your code worked on my local machine with some sample data… I tried replicating the data as if everything was a string and it worked. But I suggest you type juggle and deal with appropriate data types.

    This worked for me as well:

    // $weight = (float) $_POST["txtWeight"];
    // $height = (float) $_POST["txtHeight"];
    // $unit = (string) $_POST["optUnit"];
    // Our Sample Data
    $weight = (float) '185.33';
    $height = (float) '5.11';
    $unit = (string) ' ';
    $message = ' ';
    
    switch ($unit) {
      case 'metric':
       $bmiCalc = $weight/($height*$height);
       echo $message = 'Metric BMI: ' . $bmiCalc;
      break;
    
      default:
        $bmiCalc = ($weight * 703)/($height * $height);
        echo $message = 'Imperial BMI: ' . $bmiCalc;
      break;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.