PHP if语句:所有条件均已批准

PHP if语句:所有条件均已批准,php,if-statement,Php,If Statement,我已经检查了脚本中的一个条件,但在if语句中遇到了问题。请检查我的代码: function __construct($user=null) { parent::__construct(); require_once 'model/chanell_model.php'; $this->theme->msg='.....'; $this->db = new chanell_model(); if ($us

我已经检查了脚本中的一个条件,但在
if
语句中遇到了问题。请检查我的代码:

function __construct($user=null)
    {
       parent::__construct();
       require_once 'model/chanell_model.php';
       $this->theme->msg='.....';
       $this->db = new chanell_model();
       if ($user==null && !parent::isLog()){
           $this->themes=2;
       }elseif ($user===null){
           echo 'print';
       $this->db->getChanellData();

       }elseif($user!==null){
           echo 'print';

       }else{
           $this->db->getChanellData($user);
    }
}
怎么可能同时
$user==null
$user!=空
。 在此代码中,两个
echo
命令都将起作用

我还使用了
==
而不是
==

对不起我的英语。任何帮助都会很好。

深呼吸

if ($user===null){ echo 'bye'; }else{ echo 'hi'; } 
如果这段代码将同时写“再见”和“你好”,那么是时候更改您的计算机了……:)

更严重的是

尝试调试您的代码,并将成为您的终身朋友:) 对于即时测试,您可以按如下方式运行构造函数

function __construct($user = NULL) {
        parent::__construct();
        require_once 'model/chanell_model.php';
        $this->theme->msg   =   '.....';
        $this->db           =   new chanell_model();

        $debug          = TRUE;
        $executionId    = uniqid();
        $debugMessage   = '';

        if ((is_null($user)) && (!parent::isLog())){
            $this->themes=2;

            if ($debug) {
                $debugMessage .= "First condition executed, id: <" . $executionId . ">\n";
            }
        }
        elseif ($user===null){
            echo 'print';
            $this->db->getChanellData();
            if ($debug) {
                $debugMessage .= "Second condition executed, id: <" . $executionId . ">\n";
            }
        }
        elseif($user!==null){
            echo 'print';
            if ($debug) {
                $debugMessage .= "Third condition executed, id: <" . $executionId . ">\n";
            }
        }
        else{
            $this->db->getChanellData($user);
            if ($debug) {
                $debugMessage .= "Fourth condition executed, id: <" . $executionId . ">\n";
            }
        }
        if ($debug) {
            print $debugMessage;
        }
    }
function\uuuu构造($user=NULL){
父项::_构造();
需要_once“model/chanell_model.php”;
$this->theme->msg='…';
$this->db=new chanell_model();
$debug=TRUE;
$executionId=uniqid();
$debugMessage='';
如果((为null($user))&&(!parent::isLog()){
$this->themes=2;
如果($debug){
$debugMessage.=“执行的第一个条件,id:\n”;
}
}
elseif($user==null){
回声“打印”;
$this->db->getChannellData();
如果($debug){
$debugMessage.=“执行第二个条件,id:\n”;
}
}
elseif($user!==null){
回声“打印”;
如果($debug){
$debugMessage.=“执行了第三个条件,id:\n”;
}
}
否则{
$this->db->getchannelldata($user);
如果($debug){
$debugMessage.=“已执行第四个条件,id:\n”;
}
}
如果($debug){
打印$debugMessage;
}
}
您将看到,同一个执行id(由uniid模拟)不能运行两次
此外:

  • 使用'NULL'而不是'NULL'`
  • 要测试值是否为null,请使用`is_null()`
  • 第四个条件永远不会运行

如果($user!=null){$this->db->getchannelldata($user);}否则{$this->db->getchannelldata();}在这段代码中,$this->db->getchannelldata();};运行2次您可能会被多个输出弄糊涂,因为您创建了多个对象。一个
if elseif elseif…
只能执行它的一个块,不管你设置了什么条件。@trincot是的,如果和else都起作用,那怎么可能呢?@user3721234,我给出了一个提示:你可能正在创建多个对象并误解了输出。或者,您在问题中提供了简化的代码,这些代码不会重现您的问题。添加
echo“开始”在构造函数的开头,并
回显“end”在最后,看看输出序列是什么。也许我们正在处理量子代码,它可以像薛定谔猫一样,同时为空和不为空hi:D。我总是使用xdebug:)无论如何。is_null()输出是正确的。也可以使用NULL和nothing-change:(