反转逻辑 AND 条件

发布于 2024-11-06 00:33:45 字数 573 浏览 3 评论 0原文

我正在编写以下代码:

// $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧时浪漫 2024-11-13 00:33:45

第一种,明显的方法:

if (!( (count($data) == count($data, COUNT_RECURSIVE))
    && (count(preg_grep('~^@~', $data)) > 0)) )
{
    // everything is cool, nothing to do
}

else
{
    // do something
}

第二种,正确的方法

if ((count($data) < count($data, COUNT_RECURSIVE))
 || (count(preg_grep('~^@~', $data)) == 0))
{
    // everything is cool, nothing to do
}

else
{
    // do something
}

First, obvious way:

if (!( (count($data) == count($data, COUNT_RECURSIVE))
    && (count(preg_grep('~^@~', $data)) > 0)) )
{
    // everything is cool, nothing to do
}

else
{
    // do something
}

Second, correct way

if ((count($data) < count($data, COUNT_RECURSIVE))
 || (count(preg_grep('~^@~', $data)) == 0))
{
    // everything is cool, nothing to do
}

else
{
    // do something
}
在梵高的星空下 2024-11-13 00:33:45
if (!((count($data) == count($data, COUNT_RECURSIVE)) && (count(preg_grep('~^@~', $data)) > 0)))

{
// 做某事
}

if (!((count($data) == count($data, COUNT_RECURSIVE)) && (count(preg_grep('~^@~', $data)) > 0)))

{
// do something
}

你穿错了嫁妆 2024-11-13 00:33:45
if (! (
   count($data) == count($data, COUNT_RECURSIVE) && 
   count(preg_grep('~^@~', $data)) > 0
   )

包裹整组条件并粘贴!在开始时。换行是为了可读性。我还删除了各个条件周围不必要的 ()。

if (! (
   count($data) == count($data, COUNT_RECURSIVE) && 
   count(preg_grep('~^@~', $data)) > 0
   )

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.

执手闯天涯 2024-11-13 00:33:45

我不完全清楚你在问什么,但我认为你想要:

// $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 something
}

有了这个,块执行 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:

// $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 something
}

With this, the block executes if ((count($data) == count($data, COUNT_RECURSIVE)) && (count(preg_grep('~^@~', $data)) > 0)) is false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文