简体   繁体   English

(php) 条件运算符有什么问题? 有一个函数可以确定关联数组是否有任何键

[英](php) what's wrong with the conditional operator? there's a function that founds if an associative array have any key

I want to redirect the user when the super global variable $_GET["chapter"] it's defined and it does not have a value that I didn't specify:当超级全局变量 $_GET["chapter"] 被定义并且它没有我没有指定的值时,我想重定向用户:

if(isset($_GET["chapter"])){
    $myChapter=$_GET["chapter"];
    if($myChapter!="prelude" OR $myChapter!="chapter-1"){
        header("location:what.php");   
    }
}

the problem happens when I set the super global variable $_GET["chapter"] it's defined as prelude or as chapter-1 in the link当我设置超级全局变量 $_GET["chapter"] 时,问题发生了,它在链接中被定义为前奏或第 1 章

chapter=prelude

the page redirect me to what.php (the error page).该页面将我重定向到 what.php(错误页面)。 If I modify the conditional with a just one condition, the redirect function (header) does not redirect me to what.php.如果我只用一个条件修改条件,则重定向函数(标头)不会将我重定向到 what.php。 It works有用

if(isset($_GET["chapter"])){
    $myChapter=$_GET["chapter"];
    if($myChapter!="prelude"){
        header("location:what.php");
    }
}

if a put a "else if" or another "if" the conditional left to work. if a 放一个“else if”或另一个“if”条件就可以工作了。

Last question: there is function that found if an associative array have any key?最后一个问题:有没有找到关联数组是否有任何键的函数?

The condition you have will always return true.您拥有的条件将始终返回 true。 If, eg, $myChapter is prelude , it won't be equal to chapter-1 .例如,如果$myChapterprelude ,它将不等于chapter-1 It looks like you meant to use the and logical operator, not or :看起来您打算使用and逻辑运算符,而不是or

if ($myChapter != "prelude" and $myChapter != "chapter-1") {
    // Here ----------------^
    header("location:what.php");   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM