"女性"的计算通过将她的身高乘以英寸乘以3.5并减去108来找到选项。如果"男性"选择将通过将他的身高乘以4英寸并减去128来找到。我不知道如何将其转换为php函数。按下提交按钮即可处理此计算。我的目标是计算给定性别和身高的理想体重。
目前我无法显示$ result。有人看到我的错误吗?
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
if(isset($_POST['submit'])) {
$gender = isset($_POST['gender']) ? $_POST['gender']: 0;
$height = (int)$_POST['height'];
switch ($gender) {
case 2:
$result = ($height * 3.5) - 108;
break;
case 1:
$result = ($height * 4) - 128;
break;
default:
$result = 0;
}
echo "Ideal Weight:", $result;
}
?>
<html>
<div align="center">
<body>
<form name="form" method="post" action="<?php echo $PHP_SELF;?>">
Select Your Gender: <select name="gender">
<option value=""></option>
<option value="1">Male</option>
<option value="0">Female</option>
</select>
<br><br>
Enter Your Height: <input type="number" name="height">
<br><br>
<input type="submit" name="submit" value="Calculate"/>
</form>
</body>
</div>
</html>
答案 0 :(得分:1)
试试这个:
<?php
if(isset($_POST['submit'])) {
$gender = isset($_POST['gender']) ? $_POST['gender']: 0;
$height = (int)$_POST['height'];
switch ($gender) {
case 1:
$multiplier = 3.5;
$substract = 108;
break;
case 0:
default:
$multiplier = 4;
$substract = 128;
break;
}
$result = ($height * $multiplier) - $substract;
//Do whatever you want to do with $result
}
?>
这只是一个粗略的例子,没有完成转换为英寸,只是为了显示计算结果的方法。
答案 1 :(得分:0)
我是否理解这一点,这是你想要计算高度的方式吗?
if(isset($_POST['submit'])) {
$gender = isset($_POST['gender']) ? $_POST['gender']: 0;
$height = (int)$_POST['height'];
switch ($gender) {
case 2:
$result = ($height * 3.5) - 108;
break;
case 1:
$result = ($height * 4) - 128;
break;
default:
$result = 0;
}
echo $result;
}