How to do an || statement inside a ternary?
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?
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" ? : ;
$somevar = (!in_array($progress_date, array('0000-00-00 00:00:00','//')) ? $progress_date : 'NA';
You're not using the or properly.
$somevar = ($progress_date != '0000-00-00 00:00:00' && $progress_date !='//') ? $progress_date:'NA';
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;
精彩评论