我最近开始使用PHP,但我无法弄清楚如何echo
结果。任何帮助将不胜感激。
<html>
<head>
<style>
input[type=submit]{
background-color: #4CAF50;
border: none;
color: white;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
input[type=text]{
border: 3px solid black;
}
</style>
</head>
<body>
<form action="fitness1.php" method="post">
<b>weight:</b> <input type="text" name="weight"><br>
<br>
<b>height:<b> <input type="text" name="height"><br>
<br>
<input type="submit">
</body>
</html>
这是PHP代码:
<html>
<body>
<?php
$WEight = $_Post['weight'];
$HEight = $_post['height'];
if (is_numeric($WEight)&& is_numeric($HEight)){
$bmi= $WEight / $HEight * $HEight;
}
echo ("$bmi");
else {
echo "please enter your weight and height";
}
?>
</body>
</html>
答案 0 :(得分:2)
变量的情况在PHP中很重要。您正在寻找此:
$WEight = $_POST['weight'];
$HEight = $_POST['height'];
然后,这是我们将使用的逻辑:
if (is_numeric($WEight)&& is_numeric($HEight)) {
$bmi = $WEight / $HEight * $HEight;
echo $bmi;
} else {
echo "please enter your weight and height";
}
您的echo
需要位于if (...) { ... }
区块内。
修改强>:
根据要求,我将如何改进它:
HTML:
<html>
<head>
<style>
input[type=submit]{
background-color: #4CAF50;
border: none;
color: white;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
input[type=text]{
border: 3px solid black;
}
</style>
</head>
<body>
<form action="fitness1.php" method="post" />
<b>Weight:</b> <input type="text" name="weight" /><br />
<br />
<b>Height:<b> <input type="text" name="height" /><br />
<br />
<input type="submit" />
</body>
</html>
PHP:
<html>
<body>
<?php
$weightInKg = (int) $_POST['weight'];
$heightInCm = (int) $_POST['height'];
if ($heightInCm && $weightInKg) {
$bmi = $weightInKg / pow($heightInCm, 2);
echo "Your BMI is ${bmi}.";
} else {
echo "Please enter your height and weight as a number greater than 0.";
}
?>
</body>
</html>
答案 1 :(得分:0)
错误消息的主要问题是:
echo("$bmi"); // this is wrong you don't need brackets.
用
替换它echo $bmi;
这是错误的原因但是如果建议将echo $ bmi移入内部,如果条件或初始化$ bmi,否则如果条件没有通过则可以得到未定义的变量错误。