我在下面有这个表单代码,它工作正常,但我想知道是否有更短的方法将结果回显到 html 段落中。例如,我宁愿只说要回显的函数,而不命名构成该函数的所有变量(例如,就像在最后的 p 标记中所做的那样)。
<?php
//variables and result
function bmi($height, $weight, $waist) {
$height = floatval($height);
$weight = floatval($weight);
$waist = floatval($waist);
return round($weight / ($height * $waist),1);
}
?>
<html>
<head>
<title>Test</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<form action="" method="POST">
<h4>ABI 1</h4>
<input id="height" name="height" type="text" placeholder="height in meters or feet" value="<?php echo isset($_POST['height']) ? $_POST['height'] : ''; ?>" />
<br/>
<br/>
<input id="weight" name="weight" type="text" placeholder="weight in kgs or lbs" value="<?php echo isset($_POST['weight']) ? $_POST['weight'] : ''; ?>" />
<br/>
<br/>
<input id="waist" name="waist" type="text" placeholder="waist in inches or cm" value="<?php echo isset($_POST['waist']) ? $_POST['waist'] : ''; ?>" />
<br/>
<br/>
<input class="submit" type="submit" value="Submit"/>
<?php
//handles if empty or 0 input
if (!empty($_POST['height']) && !empty($_POST['weight']) && !
empty($_POST['waist'])) :
?>
<p id="result">Your score is <?php echo bmi($_POST['height'], $_POST['weight'],$_POST['waist']);
endif;
?>
</p>
</form>
</body>
</html>
噜噜哒