BMI kalkulator

WPTurbo » Snippets » BMI kalkulator
0

Created with:

Visibility: 

public

Creator: Stan Dab

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
function wpturbo_calculate_bmi($weight, $height) {
    // Sanitize and validate input
    $weight = filter_var($weight, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
    $height = filter_var($height, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
    
    if (
        $weight === false || $weight === '' ||
        $height === false || $height === ''
    ) {
        // Handle invalid input
        return false;
    }
    
    // Convert height from centimeters to meters
    $height = $height / 100;
    
    // Calculate BMI
    $bmi = $weight / ($height * $height);
    
    return $bmi;
}

// Example usage:
$weight = $_POST['waga'] ?? '';
$height = $_POST['wzrost'] ?? '';

$bmi = wpturbo_calculate_bmi($weight, $height);

if ($bmi === false) {
    echo "Nieprawidłowe dane. Proszę wprowadzić prawidłową wagę i wzrost.";
} else {
    echo "Twoje BMI wynosi: " . $bmi;
}
?>
				

Explanation:

I have updated the result message to "Twoje BMI wynosi" to match the desired output. The rest of the code remains the same.

Please note that if you are using this code in a WordPress plugin, it is recommended to wrap the code inside a function and hook it to an appropriate action or filter hook. This ensures that the code is executed at the appropriate time in the WordPress execution flow.

Register an account to save your snippets or go Pro to get more features.