繁体   English   中英

我应该使用&&或||

[英]what i should use && or ||

这是我的代码

<?php

if( $_SERVER['REQUEST_URI']!="/index.php?task=join&" || $_SERVER['REQUEST_URI']!="/index.php?task=join&step=1" 
|| $_SERVER['REQUEST_URI']!="/index.php?task=join&step=2" ) {
    include ("inc/js/script.php");
}

?>

我需要说“如果REQUEST_URI!= /index.php?task=join&'或'/index.php?task=join&step=1'或'/index.php?task=join&step=2 >>> include inc / js /script.php

当我使用&&时,它可以正常工作,但是我认为正确的答案应该是||。

请问怎么了?

在我看来,您想要“ AND”-或者您可以将其含义全部更改为:

if (! (uri == first || uri == second || uri == third) )

如果考虑一下,URI不能同时是“ /index.php?task=join&” “ /index.php?task=join&step=1”,因此它必须不等于其中之一-因此使用|| 在您当前的代码中将始终返回true。

|| 表示or&&表示and 如果or要使用的是,请使用||

但是,如果使用|| ,条件将始终为true ,因为$_SERVER['REQUEST_URI']始终与"/index.php?task=join&""/index.php?task=join&step=1" ,因为它可以两者都不是。

因此,我认为您实际上正在寻找and条件- &&

另外,您应该使用strcmp进行字符串比较。

答案是&&因为在比较同一变量时 AND您希望它为AND ,因此不等于不同的值。

您希望变量not this, not that, *AND* not the other

如果使用OR|| ),则可以将其作为其中之一,但前提是不是全部(显然不是)。

如果我了解您想要的内容:建议使用$ _GET并按以下方式进行操作:

<?php
 if ($_SERVER["PHP_SELF"] == "/index.php" && $_GET['task'] == "join" && (isset($_GET['step'] && ($_GET['step'] == 1 || $_GET['step'] == 2)))
 include_once("inc/js/script.php");



?>

可以用英语描述自己想做什么,但是当您开始编写代码时,会出现翻译错误。

这是您说的想要的行为:

如果REQUEST_URI!= /index.php?task=join&'或'/index.php?task=join&step=1'或'/index.php?task=join&step=2 >>> include inc / js / script.php

当您这样说时,很明显“ not”(!=中的!)适用于所有URI。 因此,让我们打破NOT并使用方括号表明它适用于所有内容:

如果不是(REQUEST_URI = /index.php?task=join&'或'/index.php?task=join&step=1'或'/index.php?task=join&step=2)>>>包含inc / js / script。的PHP

现在,要使我们从英语过渡到接近编码,我们只需要更完整地说明一下:

if ! (REQUEST_URI == /index.php?task=join& or
        REQUEST_URI == /index.php?task=join&step=1 or
        REQUEST_URI == /index.php?task=join&step=2) 
    include inc/js/script.php

这实质上是Jon Skeet提出的解决方案。

进一步说明,这就是将|| s更改为&& s时代码可以工作的原因。 您实际上已经重新发现了众所周知的逻辑规则,即De Morgan定律(在Salman A的评论中提到),它具有两种形式(其中“ <==>”表示“如果且仅当”):

  1. !(A || B)<==>!A &&!B
  2. !(A && B)<==>!A || !B

根据德摩根定律的表格1,因此上面的代码与

if (!REQUEST_URI == /index.php?task=join& and
        !REQUEST_URI == /index.php?task=join&step=1 and
        !REQUEST_URI == /index.php?task=join&step=2) 
    include inc/js/script.php

这与

if (REQUEST_URI != /index.php?task=join& and
        REQUEST_URI != /index.php?task=join&step=1 and
        REQUEST_URI != /index.php?task=join&step=2) 
    include inc/js/script.php

您是否希望uri为:

a)不等于任何这些东西(&&)

要么

b)不等于其中一个或多个(或全部)(||)

您要&&我怀疑。

暂无
暂无

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

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