如何做||三元体内的语句?

发布于 2024-11-29 01:52:13 字数 405 浏览 2 评论 0原文

我现在所拥有的是:

$somevar = ($progress_date != ('0000-00-00 00:00:00' || '//'))?$progress_date:'NA';

并且它永远不会吐出$progress_date。它默认总是打印“NA”。

这样做并使用 less () 来分隔事物

$somevar = ($progress_date != '0000-00-00 00:00:00' || '//')?$progress_date:'NA';

使得 $progress_date 总是吐出,即使日期设置为一串 0 也是如此。

有没有办法使用三元语句来捕获空白日期和设置为 0 的日期,以便打印出“NA”?

What I have right now is:

$somevar = ($progress_date != ('0000-00-00 00:00:00' || '//'))?$progress_date:'NA';

and it doesn't ever spit out $progress_date. It defaults to always printing 'NA' instead.

Doing this and using fewer () to separate things

$somevar = ($progress_date != '0000-00-00 00:00:00' || '//')?$progress_date:'NA';

makes it so $progress_date always spits out, even when the date is set to a string of 0's.

Is there a way using a ternary statement to catch both blank dates and dates set to 0 so that 'NA" gets printed out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

鸩远一方 2024-12-06 01:52:13

看起来您真正想要的是一对带有 && 的条件。

$somevar = ($progress_date != '0000-00-00 00:00:00' && $progress_date != '//')?$progress_date:'NA';

每个布尔比较需要有两侧,因此您不能这样做:

// Won't do what you expect
$somevar = $progress_date != ('thing1' || 'thing2') ? : ;

而是在两侧进行完整比较。大声读出,它符合您的需要:进度日期不等于 thing1,进度日期也不等于 thing2

$somevar = $progress_date != "thing1" && $progress_date != "thing2" ? : ;

Looks like what you actually want is a pair of conditions with &&.

$somevar = ($progress_date != '0000-00-00 00:00:00' && $progress_date != '//')?$progress_date:'NA';

You need to have two sides to each boolean comparison, so you cannot do :

// Won't do what you expect
$somevar = $progress_date != ('thing1' || 'thing2') ? : ;

Instead make the full comparison on both sides. Read out loud, it makes sense as what you need: Progress date is not equal to thing1 and progress date is also not equal to thing2

$somevar = $progress_date != "thing1" && $progress_date != "thing2" ? : ;
白芷 2024-12-06 01:52:13
$somevar = (!in_array($progress_date, array('0000-00-00 00:00:00','//')) ? $progress_date : 'NA';
$somevar = (!in_array($progress_date, array('0000-00-00 00:00:00','//')) ? $progress_date : 'NA';
花海 2024-12-06 01:52:13

您没有正确使用 或 。

$somevar = ($progress_date != '0000-00-00 00:00:00' && $progress_date !='//') ? $progress_date:'NA';

You're not using the or properly.

$somevar = ($progress_date != '0000-00-00 00:00:00' && $progress_date !='//') ? $progress_date:'NA';
明天过后 2024-12-06 01:52:13

我认为它应该是

($progress_date != '0000-00-00 00:00:00' && $progress_date !='//')

|| 不能按您的预期使用,因为它总是评估为布尔值。这样 $progress_date != ('0000-00-00 00:00:00' || '//') 实际上与以下内容相同:

$temp = '0000-00-00 00:00:00' || '//'; //gives true
$progress_date != $temp;

I think it should be

($progress_date != '0000-00-00 00:00:00' && $progress_date !='//')

|| can't be used as you expected, because it always evaluates as boolean. That way $progress_date != ('0000-00-00 00:00:00' || '//') is effectively the same as:

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