我有以下开关语句。我想知道如何制作"如果不是空"的情况。我很确定这是有效的,但我在 interwebz 上找不到任何关于它的内容。
// Switch on Count
switch($this->_count) {
case !"":
return true;
break;
default:
return false;
break;
}
因此,写入 case !"":
意味着当$this->_count
不为 null 时返回 true,否则返回 false。我不能用那些看起来很奇怪的 if 语句之一来做到这一点吗?用"?"和":"写的那个?我没怎么用过。任何帮助,不胜感激。谢谢!
不要试图过度设计这些东西,只需使用简单的 IF 语句:
if(is_null($this->_count)) {
return false;
} else {
return true;
}
或者超级简单:
return !is_null($this->_count);
不过,仅供参考,您可以使用 switch 来计算这样的表达式:
switch(true) {
case is_null($this->_count):
return true; //dont need break; since return ends execution
default:
return false;
}