How to make an IMC calculator?

Asked

Viewed 1,609 times

0

I’m trying to create functions without searching on the Internet, just with the notion of the things I’ve learned, but I’m not able to make a calculator, a broken number. What am I missing?

$peso = $_POST['peso'];
$altura = $_POST['altura'];

function imc($altura, $peso){
$altura = str_replace(',', '.', $altura);
$result = $altura * $altura / $peso;
return $result;

}

echo"seu imc é: " .imc($altura , $peso);
?>
  • BMI is not = weight in kg / (height in metres * height in metres)

2 answers

3

IMC = weight in kg divided by height times height, which is the response of Caymmi.

But there are other operations that return the same result:

1 - using pow(x,y) function returns x high to the power of y, in the case of $squared height. See example in ideone

$peso = $_POST['peso'];
$altura = $_POST['altura'];

function imc($altura, $peso){
$altura = str_replace(',', '.', $altura);
$result = $peso/pow($altura, 2);
return $result;

}

echo"seu imc é: " .imc($altura , $peso);

2 - Inverse of a division. See example on ideone

could use too $result = 1/(pow($altura, 2)/ $peso); that would return the same result.

     pow($altura, 2)              $peso
1 ÷  _______________  =  1 x ________________ = $peso/pow($altura, 2)
         $peso                pow($altura, 2)

3 - From PHP 5.6 you may prefer to use the operator ** See example on ideone

$peso = $_POST['peso'];
$altura = $_POST['altura'];

function imc($altura, $peso){
$altura = str_replace(',', '.', $altura);
$result = $peso/$altura**2;
return $result;

}

echo"seu imc é: " .imc($altura , $peso);

the result can be formatted using the function number_format()

  • A math class for -1 :-)

1

First you have to multiply height by height and then divide weight by height.

  function imc($altura, $peso){
$altura = str_replace(',', '.', $altura);
$altura = $altura * $altura;
$result = $peso / $altura;
return $result;}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.