Question

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
}
Was it helpful?

Solution

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
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top