使用逻辑运算符“or”返回始终为真


Using logical Operator 'or' return always true

hei,所以我在这里要做的是验证我的函数的两个来源,如果设置为$this->post["search"]使用它,或者如果设置为$this->post["id"]使用id,但不能同时使用id,但不能同时使用两者:

($this->post["search"] or $this->post["id"])

如果我转储,我会得到一个带有真答案的布尔值,为什么?因此,如果我有一个像 sql 字符串这样的长字符串,我不想使用 elseif 的,我想更像:

$this->sql->cell("select search_for('" . $this->post["search"] or $this->post["id"] . "') as response;");

您可能应该根据设置的内容传递控制流:

if ( isset($this->post["search"]) ) {
  // deal with search
} else if ( isset($this->post["id"]) ) {
  // deal with id
} else { 
  // gracefully reject dealing: nothing to do
}

顺便说一句,isset返回空字符串的true

$test = array( 'a' => '' );
var_dump(isset($test['a']));
//⇒ bool(true)

若要确定$_POST参数是否具有值,请使用:

!empty($_POST['id'])

希望对您有所帮助。