-4

当我单击 y 链接时,它会转到 x。为什么会这样?

<a href="test.php?hello=x">x</a>
    <a href="test.php?hello=y">y</a>
    <?php
    if(isset($_REQUEST['hello']) == 'x')
    {
    echo 'x';
    }
    else if(isset($_REQUEST['hello']) == 'y'){
    echo 'y';
    }
    else
    {
    echo "else";
    }
4

6 回答 6

3

尝试

if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) == 'x')   )
于 2012-11-19T05:53:04.100 回答
2

The isset function returns either true or false and you are comparing that return value with strings 'x' and 'y'.

Since you are using == and not ===, true == 'x' will return ture.

To fix this first you need to check if the variable is set and only then compare it.

if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) === 'x'))
于 2012-11-19T05:52:06.220 回答
0

issetreturn trueor false,在这两个例子hello中都设置为something,所以isset会 return true(不等于xor y

希望这会有所帮助。

于 2012-11-19T05:53:07.220 回答
0
     <?php
        if(isset($_REQUEST['hello']) &&  $_REQUEST['hello']== 'x')
        {
        echo 'x';
        }
        else if(isset($_REQUEST['hello']) && $_REQUEST['hello'] == 'y'){
        echo 'y';
        }
        else
        {
        echo "else";
        }
?>
于 2012-11-19T05:53:12.210 回答
0

isset 将检查请求是否设置返回 0 或 1

<a href="test.php?hello=x">x</a>
<a href="test.php?hello=y">y</a>
<?php
if($_REQUEST['hello'] == 'x')
{
echo 'x';
}
else if($_REQUEST['hello'] == 'y'){
echo 'y';
}
else
{
echo "else";
}
于 2012-11-19T05:55:19.613 回答
0
<?php  $myvar = $_REQUEST['hello'];


if($myvar == 'x')
{
echo 'x';
}
else if($myvar == 'y')
{
    echo 'y';
}
else
{
    echo 'else';
}

?>

试试这个

于 2012-11-19T07:02:00.280 回答