我们可以轻松地消除错误(请参阅@John_Conde 的回答),但这里更大的问题是您的代码没有意义。
您似乎正在尝试在 REQUEST_URI 中的任何位置查找两个字符串之一。但这不是 URI 的结构方式。
这是您可能的意思的严格翻译。然后我会解释为什么它是错误的。
function stripos_array($haystack, $needles, $offset=0) {
foreach ($needles as $needle) {
if (FALSE!==stripos($haystack,$needle,$offset)) {
return True;
}
}
return False;
}
$pages = array("random-number-generator", "calculator");
if (stripos_array($_SERVER['REQUEST_URI'], $pages) {
echo 'active';
}
这怎么可能是您正在做的事情的正确实施?看一些样本:
stripos_array('/not-a-random-number-generator', $pages) // true!
stripos_array('/some/other/part/of/the/site?searchquery=calculator+page', $pages); // true!
stripos_array('/random-number-generator/calculator', $pages); // true!! but meaningless!!
我强烈怀疑您真正想要做的是使用一些真正的url 路由。这里有两种可能性:
使用查询参数;网址看起来像http://example.org/index.php?page=calculator
if (isset($_GET['page']) && in_array($_GET['page'], $pages)) ....
使用路径段;网址看起来像http://example.org/index.php/calculator
$path = trim($_SERVER['PATH_INFO'], '/');
$pathsegments = explode('/', $path);
if (isset($pathsegments[0]) && in_array($pathsegments, $pages)) ...