gpt4 book ai didi

php - 如何修复num验证和0值错误代码以使其正常工作以及连接多个错误以立即显示

转载 作者:行者123 更新时间:2023-12-03 08:39:49 24 4
gpt4 key购买 nike

基本上,用户必须输入引诱的开始和结束值,以及他们希望图表增加的依据。这是该程序应该提取的错误的代码。我的代码不适用于
用户输入文本值而不是数字时出错
并且当用户输入零时的错误不起作用,但是如果用户输入负数...它将起作用(因此确实令人困惑)
我也不知道为什么我不能连接所有错误以允许一次显示多个错误
您能确定问题区域之外的其他区域吗?

<?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创建了一个分配而不是一个条件,因此它将不起作用,因为该分配始终为 true还请记住先前的类型转换,因此也可以通过Triple =来检查类型
    代替
    else if($incr = "0")
    else if($incr === 0)
  • 由于是double的类型,因此它也永远不会成立,因为$num始终是数字
  • else if(!is_numeric($num))在上述更改之后,应将其更改为
    else if($num < 0)
    代替

    关于php - 如何修复num验证和0值错误代码以使其正常工作以及连接多个错误以立即显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62688882/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com