Domanda

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
}
È stato utile?

Soluzione

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
}

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top