如果第一个参数有效,php if语句看起来就不是第二个参数

如果第一个参数有效,php if语句看起来就不是第二个参数,php,Php,我注意到,如果第一个参数为true,PHP不会运行'if statement'的第二个或其他参数 if($this->sessions->remove("registered_id") or $this->sessions->remove("user_id")){ echo "you have logged out"; }else { echo "wth?"; } 这就是我如何使用if。这里还有sessions类的remove函数 pub

我注意到,如果第一个参数为true,PHP不会运行'if statement'的第二个或其他参数

if($this->sessions->remove("registered_id") or $this->sessions->remove("user_id")){
        echo "you have logged out";
}else {
        echo "wth?";
}
这就是我如何使用if。这里还有sessions类的remove函数

public function remove($key){
        if(isset($_SESSION[$key])){
            unset($_SESSION[$key]);
            return true;
        }
        else
        {
            return false;
        }
    }

我要做的是运行这两个参数。。我希望我能说出这个问题

如果其他参数指的是第二个条件,则使用AND而不是OR


如果其他参数指的是else,则使用单独的If语句

编辑

如果要执行这两条语句,请使用位运算符,请参阅本手册:

比如:

if(a | b){

}

这将同时执行a和b,但仍然是“或”比较。

您需要执行这两个函数,存储它们各自的结果,然后对这些结果进行测试

$resultA = $this->sessions->remove("registered_id");
$resultB = $this->sessions->remove("user_id");

if ($resultA or $resultB)
{
     …

根据设计,第二条语句不会被执行,因为它的结果将是不相关的。

这个结果是可以预期的。这就是我们要做的

您需要使用&&or和来实现您似乎在寻找的目标:

if ($this->sessions->remove("registered_id") && $this->sessions->remove("user_id")) {
原因如下:

&&or和关键字意味着所有计算都必须返回true。因此:

if ($a && $b) {
    // $a and $b must both be true
    // if $a is false, the value of $b is not even checked
}
if ($a || $b) {
    // Either $a or $b must be true
    // If $a is false, the parser continues to see if $b might still be true
    // If $a is true, $b is not evaluated, as our check is already satisfied
}
| | or或关键字意味着任何一个计算都必须返回true。因此:

if ($a && $b) {
    // $a and $b must both be true
    // if $a is false, the value of $b is not even checked
}
if ($a || $b) {
    // Either $a or $b must be true
    // If $a is false, the parser continues to see if $b might still be true
    // If $a is true, $b is not evaluated, as our check is already satisfied
}

因此,在您的情况下,如果$this->sessions->removegistered\u id成功地完成了这项任务,则不会调用$this->sessions->removeuser\u id,因为我们的检查已经对第一次调用的结果感到满意。

那么您想运行if和else吗?不,我想同时删除registered_id和user_id,而不在另一个中使用if。PHP和大多数语言都只会计算if条件中的子句,直到找到一个在使用or时通过的子句。一旦它找到一个通过的,就不需要计算任何其他内容。但是它的注册id可能是空的。那么我需要使用或。。你不明白我在说什么……好吧,对不起,我不明白你一开始想做什么。我编辑了这篇文章,希望你能找到我想要的。按位排他或使用按位运算符防止短路基本上只是想让自己以后感到困惑P如果您想计算这两个表达式,那么只需使它们成为单独的语句。