How to make an imc calculator? [duplicate]

0

I'm trying to create functions without searching the internet, only with the notion of the things I've learned, but I'm not able to make an imc calculator, a number that is broken. 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);
?>
    
asked by anonymous 27.08.2017 / 01:50

2 answers

3

By what you see on the internet 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 raised to the power of y, in the case $ height squared. See example on 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 also use $result = 1/(pow($altura, 2)/ $peso); which 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 in 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 ()

    
27.08.2017 / 03:07
0

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;}
    
27.08.2017 / 02:14