我无法弄清楚为什么函数为输入“Dash”返回false,数组中的每个其他值在测试时都会返回true。
$Valid_Links=array("Dash","Profile","Messages","Friends","Blogs","Galleries","Calendar","Settings","Logout");
//function for handling $_GET['content'] variables (uri)
function mainHandler(){
echo '<SECTION id="content_wrapper">';
global $Valid_Links;
if(isset($_GET['content'])){
if(array_search(strtolower($_GET['content']), array_map('strtolower', $Valid_Links))){
$doc=strtolower($_GET['content']);
require "main/$doc.php";
}else{echo 'false';}
}
else{
if(checkLogin()){
require 'main/dash.php';
}
else{
require 'main/signup.php';
}
}
echo '</SECTION>';
}
答案 0 :(得分:1)
因为array_search
返回找到的元素的位置,在这种情况下是0
(第一个元素),但是0
在放入if语句时评估为FALSE。
<强>解决方案:强>
将!== FALSE
添加到您的情况中:
if(array_search(strtolower($_GET['content']), array_map('strtolower', $Valid_Links)) !== FALSE){
<强>更新强>
就像@VolkerK提到的那样,如果你努力实现最佳实践,你可以在IF语句中放入FALSE。这样可以减轻意外分配变量的问题,而不是比较它们:
if(FALSE !== array_search(strtolower($_GET['content']), array_map('strtolower', $Valid_Links))){