开发者

Debugging a Simple IF Statement in PHP

Code

DOES NOT WORK --

$u开发者_如何学Python = $_SESSION['username'];
while($responseanswer=mysqli_fetch_array($rquery)){

if($responseanswer['onuser']=='$u'&&$responseanswer['response']=='')
{
    echo "awesome";
}
}

WORKS

$u = $_SESSION['username'];
    while($responseanswer=mysqli_fetch_array($rquery)){

    if($responseanswer['onuser']&&$responseanswer['response']=='')
    {
        echo "awesome";
    }
    }

How do I resolve this? $u is perfectly fine. Thanks.


Single quotes are not interpolated for variables inside them. This should work:

if($responseanswer['onuser']==$u && $responseanswer['response']=='')

But one thing to note, I'd highly suggest that you format your cod ebetter. Include spaces where appropriate, and always indent for scope. So that code would become:

$u = $_SESSION['username'];
while ($responseanswer=mysqli_fetch_array($rquery)) {
    if ($responseanswer['onuser'] == $u && $responseanswer['response'] == '') {
        echo "awesome";
    }
}


I believe

$responseanswer['onuser']=='$u'

should be

$responseanswer['onuser']==$u

without the single quotes around $u.


There's a big difference between ' and ".

Try this:

$a = "foo";
echo('$a' . " $a"); // You'll get: $a foo

Anyway, don't put your variables into strings just to compare them. Just use $responseanswer['onuser']==$u.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜