-3

在这里完成 PHP noob 并返回错误,因为嵌套if块很大。当我更改代码时,错误会有所不同,但它始终与if语句有关。执行此语句的正确方法是什么?

这是不正确的if块:

if(!empty($_GET['x2'])) and (!empty($_GET['x'])) and (!empty($_GET['num']))
    {
    $xsqrd = $_GET['x2'];
    $x = $_GET['x'];
    $num = $_GET['num'];
    if(($xsqrd*2)==0.0)
    {
    print $errdivzero;
    }
    else if(sqrt(pow($x,2)-(4*$xsqrd*$num)))
    {
    print $errsqrtmin1;
    }
    else
    {
    $ans1 = (-$x)+(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
    $ans2 = (-$x)-(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
    }
    }
4

3 回答 3

3
    if(!empty($_GET['x2']) && !empty($_GET['x']) && !empty($_GET['num']))
    {
        $xsqrd = $_GET['x2'];
        $x = $_GET['x'];
        $num = $_GET['num'];
        if(($xsqrd*2)==0.0)
        {
            print $errdivzero;
        }
        else if(sqrt(pow($x,2)-(4*$xsqrd*$num)))
        {
            print $errsqrtmin1;
        }
        else
        {
            $ans1 = (-$x)+(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
            $ans2 = (-$x)-(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
        }
    }

您有额外(if条件,并且您声明and了哪个不是 php 函数。您需要替换and&&.

现在它工作正常,没有任何错误消息。

于 2013-03-09T10:20:53.917 回答
1
if(!empty($_GET['x2']) && (!empty($_GET['x']) && (!empty($_GET['num']))
 //                  ^ extra bracket         ^ extra bracket
{
    $xsqrd = $_GET['x2'];
    $x = $_GET['x'];
    $num = $_GET['num'];
    if(($xsqrd*2)==0.0)
    {
      print $errdivzero;
    }
    else if(sqrt(pow($x,2)-(4*$xsqrd*$num)))
    {
      print $errsqrtmin1;
    }
    else
    {
      $ans1 = (-$x)+(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
      $ans2 = (-$x)-(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
    }
 }

您在上面突出显示的位置有额外的右括号。也替换and&&

于 2013-03-09T10:20:39.447 回答
0

好吧,让我们检查一下您的括号:

if(!empty($_GET['x2'])) and (!empty($_GET['x'])) and (!empty($_GET['num']))
  ^                   ^     

第二个突出显示的括号关闭if语句 - 这将导致错误,您应该删除不必要的括号,如下所示:

if(!empty($_GET['x2']) and !empty($_GET['x']) and !empty($_GET['num']))

将来,您应该仔细检查每个左括号的右括号是否在您期望的位置,并且不要过度使用括号的数量。

于 2013-03-09T10:21:02.213 回答