开发者

Is there something wrong with PHP OR operator?

My dump variables appear on their pages:

if ($_SERVER['REMOTE_ADDR'] == '44.71.45.98' OR '44.71.45.99') {
  //blah blah
}else{
  //blah
}
开发者_JAVA技巧

I know this is wrong but when I tried..it's working to my another computer.It didn't prompt me an error even if i know that this is wrong...

Usually I code or like these:

//this is correct
if(isset($a) or isset($b)){

}


Yes, it should be:

if ($_SERVER['REMOTE_ADDR'] == '44.71.45.98' or $_SERVER['REMOTE_ADDR'] == '44.71.45.99') ...

'or' (or '||') groups together boolean (true or false) expressions (such as the comparisons using '=='). In your original code, it was converting the second IP string to a boolean -- the value true -- so the if statement would always match.


if (
$_SERVER['REMOTE_ADDR'] == '44.71.45.98'
OR
$_SERVER['REMOTE_ADDR'] == '44.71.45.99'
)


Your expression is not correct. It should be like this:

if ($_SERVER['REMOTE_ADDR'] == '44.71.45.98' OR
    $_SERVER['REMOTE_ADDR'] == '44.71.45.99')

Or how this is done by 99% of developers:

if ($_SERVER['REMOTE_ADDR'] == '44.71.45.98' ||
    $_SERVER['REMOTE_ADDR'] == '44.71.45.99')


I prefer to use an array of IPs (if more than one)

if(in_array($_SERVER['REMOTE_ADDR'], array('44.71.45.98', '44.71.45.99', etc))


here you need to use logical OR operator..

your code should be like this..

if($_SERVER['REMOTE_ADDR'] == '44.71.45.98' || $_SERVER['REMOTE_ADDR'] == '44.71.45.99'){
    code if condition is true..
}
else{
  code if condition is false..
}

can do it by this way too..

if($_SERVER['REMOTE_ADDR'] == '44.71.45.98' or $_SERVER['REMOTE_ADDR'] == '44.71.45.99')


Here I prefer to use || as OR operator. The code should be:

if ($_SERVER['REMOTE_ADDR'] == '44.71.45.98' || $_SERVER['REMOTE_ADDR'] == '44.71.45.99')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜