Created
June 2, 2023 19:04
-
-
Save simohammedhttp/e989479c66434e106f72e69b1fead790 to your computer and use it in GitHub Desktop.
Body Mass Index (BMI) Calculator, for https://www.maxipotion.com/en/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
$weight = $_POST['weight']; | |
$height = $_POST['height']; | |
// Validate inputs | |
$errors = []; | |
if (empty($weight)) { | |
$errors[] = 'Weight is required'; | |
} | |
if (empty($height)) { | |
$errors[] = 'Height is required'; | |
} | |
// Calculate BMI if inputs are valid | |
if (empty($errors)) { | |
$bmi = calculateBMI($weight, $height); | |
$category = getBMICategory($bmi); | |
// Display the result | |
echo "Your BMI is: " . $bmi . "<br>"; | |
echo "Category: " . $category; | |
} else { | |
// Display validation errors | |
foreach ($errors as $error) { | |
echo $error . "<br>"; | |
} | |
} | |
} | |
function calculateBMI($weight, $height) | |
{ | |
// Calculate BMI | |
$bmi = $weight / ($height * $height); | |
return round($bmi, 2); | |
} | |
function getBMICategory($bmi) | |
{ | |
// Define BMI categories and their ranges | |
$categories = array( | |
'Underweight' => array('min' => 0, 'max' => 18.4), | |
'Normal weight' => array('min' => 18.5, 'max' => 24.9), | |
'Overweight' => array('min' => 25, 'max' => 29.9), | |
'Obesity' => array('min' => 30, 'max' => PHP_FLOAT_MAX) | |
); | |
// Determine the category based on the BMI value | |
foreach ($categories as $category => $range) { | |
if ($bmi >= $range['min'] && $bmi <= $range['max']) { | |
return $category; | |
} | |
} | |
return 'Unknown'; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>BMI Calculator</title> | |
</head> | |
<body> | |
<h1>BMI Calculator</h1> | |
<form method="POST" action=""> | |
<label for="weight">Weight (kg):</label> | |
<input type="text" name="weight" id="weight" /> | |
<br/> | |
<label for="height">Height (m):</label> | |
<input type="text" name="height" id="height" /> | |
<br/> | |
<input type="submit" value="Calculate BMI" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment