谁能帮我解决温度转换器的回声问题 PHP
Can someone help me to solve my echo problem for my temperature converter PHP
所以这是我在 PHP
中进行温度转换的代码
<html>
<head>
<title>Temp Conversion</title>
<meta charset="utf-8">
</head>
<body>
<form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table>
<tr>
<td>Enter value to convert</td>
<td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
</tr>
<tr>
<td>Convert from:</td>
<td><select name="convertFrom" id="convertFrom" size="1">
<option disabled> Select a measurement type</option>
<option value="celsiusF">Celsius</option>
<option value="fahrenheitF">Fahrenheit</option>
<option value="kelvinF">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td>Convert to:</td>
<td><select name="convertType" id="convertType" size="1">
<option disabled> Select a measurement type</option>
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
<option value="kelvin">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
<td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
</tr>
</form>
<?php
function tempConvert($valueConvert, $convertFrom, $convertType)
{
if($convertFrom== "celciusF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*$valueConvert) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert + 273);
}
}
else if($convertFrom== "fahrenheitF")
{
if($convertType== "fahrenheit")
{
$conversion = ($valueConvert);
}
else if ($convertType== "celsius")
{
$conversion = (($valueConvert - 32) * (5/9));
}
else if ($convertType== "kelvin")
{
$conversion = ((($valueConvert - 32) * (5/9)) + 273);
}
}
else if($convertFrom== "kelvinF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*($valueConvert - 273)) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert - 273);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert);
}
}
}
if (isset($_POST['btnConvert']))
{
$valueConvert = $_POST['valueConvert'];
$convertFrom = $_POST['convertFrom'];
$convertType = $_POST['convertType'];
$conversion = tempConvert($valueConvert, $convertFrom, $convertType);
echo "The initial temperature was $valueConvert. The new temperature is $conversion $convertType.";
}
?>
</body>
</html>
当我 运行 这个 PHP 代码,然后插入值,然后按下转换按钮。似乎 $conversion 值没有显示在代码最后一部分的回显中。
(例如它只会显示为“初始温度为 32。新温度为摄氏度”)
我想知道是什么原因造成的,谁能帮帮我?
好的,感谢 Nigel Ren、ADyson 和 Barmar 的回答,看来我的代码有两个问题,第一个是缺少 return 值,第二个是打字时的错字"celciusF"(应该是celsiusF,用s代替c)
这是正确的代码
<html>
<head>
<title>Temp Conversion</title>
<meta charset="utf-8">
</head>
<body>
<form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table>
<tr>
<td>Enter value to convert</td>
<td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
</tr>
<tr>
<td>Convert from:</td>
<td><select name="convertFrom" id="convertFrom" size="1">
<option disabled> Select a measurement type</option>
<option value="celsiusF">Celsius</option>
<option value="fahrenheitF">Fahrenheit</option>
<option value="kelvinF">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td>Convert to:</td>
<td><select name="convertType" id="convertType" size="1">
<option disabled> Select a measurement type</option>
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
<option value="kelvin">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
<td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
</tr>
</form>
<?php
function tempConvert($valueConvert, $convertFrom, $convertType)
{
if($convertFrom== "celsiusF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*$valueConvert) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert + 273);
}
}
else if($convertFrom== "fahrenheitF")
{
if($convertType== "fahrenheit")
{
$conversion = ($valueConvert);
}
else if ($convertType== "celsius")
{
$conversion = (($valueConvert - 32) * (5/9));
}
else if ($convertType== "kelvin")
{
$conversion = ((($valueConvert - 32) * (5/9)) + 273);
}
}
else if($convertFrom== "kelvinF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*($valueConvert - 273)) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert - 273);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert);
}
}
return $conversion;
}
if (isset($_POST['btnConvert']))
{
$valueConvert = $_POST['valueConvert'];
$convertFrom = $_POST['convertFrom'];
$convertType = $_POST['convertType'];
$conversion = tempConvert($valueConvert, $convertFrom, $convertType);
echo "The initial temperature was $valueConvert. The new temperature is $conversion $convertType.";
}
?>
</body>
</html>
所以这是我在 PHP
中进行温度转换的代码<html>
<head>
<title>Temp Conversion</title>
<meta charset="utf-8">
</head>
<body>
<form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table>
<tr>
<td>Enter value to convert</td>
<td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
</tr>
<tr>
<td>Convert from:</td>
<td><select name="convertFrom" id="convertFrom" size="1">
<option disabled> Select a measurement type</option>
<option value="celsiusF">Celsius</option>
<option value="fahrenheitF">Fahrenheit</option>
<option value="kelvinF">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td>Convert to:</td>
<td><select name="convertType" id="convertType" size="1">
<option disabled> Select a measurement type</option>
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
<option value="kelvin">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
<td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
</tr>
</form>
<?php
function tempConvert($valueConvert, $convertFrom, $convertType)
{
if($convertFrom== "celciusF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*$valueConvert) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert + 273);
}
}
else if($convertFrom== "fahrenheitF")
{
if($convertType== "fahrenheit")
{
$conversion = ($valueConvert);
}
else if ($convertType== "celsius")
{
$conversion = (($valueConvert - 32) * (5/9));
}
else if ($convertType== "kelvin")
{
$conversion = ((($valueConvert - 32) * (5/9)) + 273);
}
}
else if($convertFrom== "kelvinF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*($valueConvert - 273)) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert - 273);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert);
}
}
}
if (isset($_POST['btnConvert']))
{
$valueConvert = $_POST['valueConvert'];
$convertFrom = $_POST['convertFrom'];
$convertType = $_POST['convertType'];
$conversion = tempConvert($valueConvert, $convertFrom, $convertType);
echo "The initial temperature was $valueConvert. The new temperature is $conversion $convertType.";
}
?>
</body>
</html>
当我 运行 这个 PHP 代码,然后插入值,然后按下转换按钮。似乎 $conversion 值没有显示在代码最后一部分的回显中。 (例如它只会显示为“初始温度为 32。新温度为摄氏度”)
我想知道是什么原因造成的,谁能帮帮我?
好的,感谢 Nigel Ren、ADyson 和 Barmar 的回答,看来我的代码有两个问题,第一个是缺少 return 值,第二个是打字时的错字"celciusF"(应该是celsiusF,用s代替c)
这是正确的代码
<html>
<head>
<title>Temp Conversion</title>
<meta charset="utf-8">
</head>
<body>
<form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table>
<tr>
<td>Enter value to convert</td>
<td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
</tr>
<tr>
<td>Convert from:</td>
<td><select name="convertFrom" id="convertFrom" size="1">
<option disabled> Select a measurement type</option>
<option value="celsiusF">Celsius</option>
<option value="fahrenheitF">Fahrenheit</option>
<option value="kelvinF">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td>Convert to:</td>
<td><select name="convertType" id="convertType" size="1">
<option disabled> Select a measurement type</option>
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
<option value="kelvin">Kelvin</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
<td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
</tr>
</form>
<?php
function tempConvert($valueConvert, $convertFrom, $convertType)
{
if($convertFrom== "celsiusF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*$valueConvert) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert + 273);
}
}
else if($convertFrom== "fahrenheitF")
{
if($convertType== "fahrenheit")
{
$conversion = ($valueConvert);
}
else if ($convertType== "celsius")
{
$conversion = (($valueConvert - 32) * (5/9));
}
else if ($convertType== "kelvin")
{
$conversion = ((($valueConvert - 32) * (5/9)) + 273);
}
}
else if($convertFrom== "kelvinF")
{
if($convertType== "fahrenheit")
{
$conversion = (((9/5)*($valueConvert - 273)) +(32));
}
else if ($convertType== "celsius")
{
$conversion = ($valueConvert - 273);
}
else if ($convertType== "kelvin")
{
$conversion = ($valueConvert);
}
}
return $conversion;
}
if (isset($_POST['btnConvert']))
{
$valueConvert = $_POST['valueConvert'];
$convertFrom = $_POST['convertFrom'];
$convertType = $_POST['convertType'];
$conversion = tempConvert($valueConvert, $convertFrom, $convertType);
echo "The initial temperature was $valueConvert. The new temperature is $conversion $convertType.";
}
?>
</body>
</html>