What's the best way to say: if conditions are true, do nothing?

if ($fromDate >= $rangeEnd || $toDate < $rangeStart) {
    // In both cases we are Out of Range, so do nothing...
} else {
   // We are in Range...do something
}
有帮助吗?

解决方案

Having to write a "do nothing" case isn't a good practice. I'd check for the opposite condition and use that block to run the code, i.e. negate the condition and remove the else block:

if ( !($fromDate >= $rangeEnd || $toDate < $rangeStart) ) {
    // We are in Range...do something
}

其他提示

Negate it :-) or build the condition differently!

if($fromDate < $rangeEnd || $toDate > $rangeStart) {
  // Do something
}

// Nothing to do here... :-)

Change your statement.

if ($fromDate < $rangeEnd || $toDate > $rangeStart) {
   // We are in Range...do something
}

nOtice the >= and < have been changed

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top