Pls advise, what is wrong with the PHP script:

<html>
<head>
<title>Calorie Calculation</title>
</head>
<body>
<?php
$calorie = ($_POST["fat"] * 9) / ($_POST["calories"] * 100);
?>
Welcome, $_POST["foodname"] contains:  <?php echo $calorie ."from fat" ;
echo "<br />";
{
if ($calorie > 30)
	echo ($_POST["foodname"]) 'contains $calorie % from fat <br />';
	echo ($_POST["foodname"]) 'exceeds the AHA recommendation';

else
	echo ($_POST["foodname"]) 'contains" $calorie "% from fat  <br />';
	echo ($_POST["foodname"]) 'is within the AHA recommendation of 30% calories from fat';

}

?>
</body>
</html>

Thanks,
Tony

Dani AI

Generated

The original snippet from produces a parse error and several logical and security problems. correctly observed that a rewrite is needed; improved input checks but kept a mistaken calculation. The actionable goals are: read and validate POST data, compute the percent of calories from fat correctly, prevent division-by-zero, format the result for display, and escape any user-supplied text.

Common issues to fix

  • Syntax and control flow: keep all PHP logic inside valid PHP blocks, use braces for multi-statement if/else blocks, and avoid stray text that looks like variables outside PHP.
  • Calculation: first compute calories-from-fat (fat grams * 9), then divide by total calories, then multiply by 100 for percent. Order matters.
  • Validation: check that inputs are numeric and that total calories > 0 before dividing.
  • Output: escape the food name to prevent XSS and format the percentage for readability.

A minimal, robust approach (illustrates validation, correct math, escaping, and formatting):

<?php
$food = trim((string)filter_input(INPUT_POST, 'foodname', FILTER_SANITIZE_STRING));
$fatGrams = filter_input(INPUT_POST, 'fat', FILTER_VALIDATE_FLOAT);
$totalCalories = filter_input(INPUT_POST, 'calories', FILTER_VALIDATE_FLOAT);

if ($food === '' || $fatGrams === false || $totalCalories === false) {
    echo 'Missing or invalid input.';
    return;
}
if ($totalCalories <= 0.0) {
    echo 'Total calories must be greater than zero.';
    return;
}

$calFromFat = $fatGrams * 9.0;
$percent = ($calFromFat / $totalCalories) * 100.0;
echo htmlspecialchars($food, ENT_QUOTES, 'UTF-8') . ' contains ' . number_format($percent, 1) . '% calories from fat.';
echo ($percent > 30.0) ? ' It exceeds the recommended range.' : ' It is within the recommended range.';
?>

Notes and references: use input filtering functions like filter_input and escape output with htmlspecialchars to avoid XSS. Format numbers with number_format. For context on recommended fat intake ranges, see the American Heart Association guidance. For PHP docs, see the manual pages on filter_input, htmlspecialchars, and number_format.

Recommended Answers

All 3 Replies

You might want to read your book or the tutorial or whatever you're using to learn PHP again because there are WAY too many things wrong with that script.

Member Avatar for Member #120589

Ouch!

<!--doctype must be declared xhtml -->
<html>
<head>
<title>Calorie Calculation</title>
</head>
<body>
<?php
if (!$_POST['foodname']) { echo 'No food selected <br /> more error checking is required'; }
else {
if (!$_POST['calories'] == 0 ){ $calorie = ($_POST['fat'] * 9) / ($_POST['calories'] * 100); } //division by zero errors?
echo $_POST['foodname'].' contains: '.$calorie.' from fat<br />';
if ($calorie > 30) { echo $_POST['foodname'].' exceeds the AHA recommendation'; }
else { echo $_POST['foodname'].' is within the AHA recommendation of 30% calories from fat'; }
}
?>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.