문제

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