基本上,用户必须输入一个诱人的开始和结束值以及他们希望图表增量的内容。 这是程序应该拾取的错误的代码。我的代码不适用于
用户输入文本值而不是数字的错误 当用户输入零时的错误不起作用,但如果用户输入负数......它有效(所以这真的很令人困惑(
我也不知道为什么我不能连接我的所有错误以允许一次显示多个错误
你能检查一下问题区域以外的其他区域以确保吗?
<?php
error_reporting(E_ERROR | E_PARSE);
try
{
if($_SERVER["REQUEST_METHOD"] == "GET"){
$start = "";
$stop = "";
$incr = "";
}
else if($_SERVER["REQUEST_METHOD"] == "POST")
{
$start=(double)trim($_POST["starting_number"]);
$stop=(double)trim($_POST["stop_number"]);
$incr=(double)trim($_POST["increment_number"]);
$num = (double)($stop-$start)/$incr;
if($start == "" | $stop == "" | $incr == "" | $num == ""){
//means the user did not enter anything
$error .= "You must enter something into the text box.";
echo $error;
}
//the conditions will cause too many iterations
else if($num>$MAX_ITERATIONS)
{
$error .= "The conditions will cause too many iterations (max. 100), therefore (for the sake of server resources) your request is denied.";
echo $error;
}
//When the user inputs the starting temperature larger than the ending temperature
else if($start > $stop)
{
$error .= "The starting temperature cannot be larger than the ending temperature, please try again";
echo $error;
}
这主要是我遇到问题的地方
//When the increment value is <=0
else if($incr = "0")
{
$error .= "You must enter a positve, non-zero increment.";
echo $error;
}
//means the user entered something, but not a number
else if(!is_numeric($num))
{
$error .= "The value entered <u>MUST</u> be a number, you entered" .$num;
echo $error;
}
我认为下面的其他一切都很好,但请检查以防万一
else
{
echo "<table border='1'>";
echo "<tr>";
echo "<th>Celcius</th>";
echo "<th>Fahrenheit</th>";
echo "</tr>";
for($i=$start; $i<=$stop; $i += $incr)
{
echo "<tr>";
echo "<td>". $i . "°</td>";
echo "<td>". (($i * 9/5) + 32 ). "°</td>";
echo "</tr>";
}
echo "</table>";
}
if($error == "")
{
$output = "";
}
else
{
$error .= "<br/>Please try again.";
}
}
}
catch(DivisionByZeroError $e)
{
echo "The text boxes cannot be blank.";
}
catch(Exception $e)
{
echo 'Please Enter correctly';
}
?>
您进行了一些类型转换,但没有正确与它们进行比较。
- 当通过
(double)
强制转换时,转换空字符串的结果是0
因此,这永远不应该是真的:
if($start == "" | $stop == "" | $incr == "" | $num == ""){
您基本上应该将其更改为
if($start === 0 | $stop === 0 | $incr === 0 | $num === 0){
用于与零的正确比较,因为这是空字符串的转换结果。
但是,给定的 0(通过 POST(在逻辑上可能是一个正确的值,因此您可以设置不同的默认值 -1
$start=(double)trim($_POST["starting_number"] ?? "-1");
$stop=(double)trim($_POST["stop_number"] ?? "-1");
$incr=(double)trim($_POST["increment_number"] ?? "-1");
if($start < 0 | $stop < 0 | $incr < 0 | $num < 0){
您创建了一个分配来$incr条件的 istead,因此它不起作用,因为分配始终是true
还要记住前面的类型转换,因此也通过三重 = 检查类型
而不是
else if($incr = "0")
做
else if($incr === 0)
- 由于要
double
的字体,这也永远不会是真的$num
因为永远是数字
else if(!is_numeric($num))
在上述更改之后,应将其更改为
else if($num < 0)
相反