PHP如果条件带和和或

时间:2014-06-05 17:15:47

标签: php if-statement

我有这个条件:

if($_SERVER['REQUEST_URI'] != '/page.php' || ($_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != '/index.php')){

        //do something, but not on index or page . php

}

它适用于索引页面,但不适用于page.php ...我做错了什么?

2 个答案:

答案 0 :(得分:5)

你可以更轻松地做到......

if( ! in_array( $_SERVER['REQUEST_URI'], array("/page.php", "/", "/index.php") ) ) {
  // do something...
}

答案 1 :(得分:1)

您使用逻辑错误

if(!($_SERVER['REQUEST_URI'] == '/page.php' || $_SERVER['REQUEST_URI'] == '/' || $_SERVER['REQUEST_URI'] == '/index.php')){

        //do something, but not on index or page . php
}

或更好

if(!in_array($_SERVER['REQUEST_URI'], ["/page.php", "/", "/index.php"])){

        //do something, but not on index or page . php
}