【发布时间】:2023-04-09 07:10:01
【问题描述】:
当前代码:
<?php
// See the AND operator; How do I simplify/shorten this line?
if( $some_variable !== 'uk' && $some_variable !== 'in' ) {
// Do something
}
?>
还有:
<?php
// See the OR operator; How do I simplify/shorten this line?
if( $some_variable !== 'uk' || $some_variable !== 'in' ) {
// Do something else
}
?>
有没有更简单(即更短)的方式来写这两个条件?
注意:是的,它们是不同的,我希望有不同的方法来缩短代码。
【问题讨论】:
-
您只删除了括号。请编辑您的示例代码以反映您的实际要求。
-
@CBroe 看到注释了吗?如果还不清楚,请告诉我。
-
关于“请编辑您的示例代码以反映您实际要求的内容”的不清楚之处?
-
@CBroe 在底部添加注释没有意义,我没有。更清楚地说,我已经进行了您建议的编辑。 编辑: 还注意到代码中的另一个错误,我已修复。
-
@CBroe 是正确的 - 第二个条件始终为 TRUE。但是对第一个的小修正:在示例中有 usde '!==',所以应该传递 TRUE 作为 in_array 的第三个参数。
!in_array($some_variable, array('uk', 'in'), true)
标签: php if-statement conditional-statements