反转逻辑 AND 条件
我正在编写以下代码:
// $data has only one dimension AND at least one of its values start with a "@"
if ( (count($data) == count($data, COUNT_RECURSIVE))
&& (count(preg_grep('~^@~', $data)) > 0) )
{
// do nothing
}
else
{
// do something
}
此条件的逻辑工作得很好,但是,我更愿意在评估第二个条件时仅当第一个条件时才能摆脱空的 if
块one 产生 true - 否则,当 $data
具有多个维度时,preg_grep()
调用将抛出以下通知:
注意:数组到字符串的转换
我知道我可以使用错误抑制运算符或其他一些hacky方法,但我感觉我错过了一些微不足道的东西。有人可以帮我吗?
I'm working on the following code:
// $data has only one dimension AND at least one of its values start with a "@"
if ( (count($data) == count($data, COUNT_RECURSIVE))
&& (count(preg_grep('~^@~', $data)) > 0) )
{
// do nothing
}
else
{
// do something
}
The logic of this condition is working just fine, however, I would prefer if I could get rid of the empty if
block while evaluating the second condition only if the first one yields true - otherwise the preg_grep()
call will throw the following notice when $data
has more than one dimension:
Notice: Array to string conversion
I know I could use the error suppression operator or some other hacky approaches, but I have the feeling that I'm missing something trivial. Can someone help me out, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一种,明显的方法:
第二种,正确的方法
First, obvious way:
Second, correct way
{
// 做某事
}
{
// do something
}
包裹整组条件并粘贴!在开始时。换行是为了可读性。我还删除了各个条件周围不必要的 ()。
Wrap the entire group of conditions and stick ! at the start. Line breaks are for readability. I removed the unnecessary () around the individual conditions as well.
我不完全清楚你在问什么,但我认为你想要:
有了这个,块执行 if
((count($data) == count($data, COUNT_RECURSIVE)) && ( count(preg_grep('~^@~', $data)) > 0)) 为 false。
I'm not entirely clear what you're asking, but I think you want:
With this, the block executes if
((count($data) == count($data, COUNT_RECURSIVE)) && (count(preg_grep('~^@~', $data)) > 0))
is false.