php - Best practice for > < and == in a function -
i have come across conundrum use bunch of ifs or switch.
to put in perspective, current project working on involves comparing width , height of image number determine if width longer height or same.
here code far:
private function compare($width, $height) { return $width - $height; }
this works might expect.
what find out if there better way this:
if($this->compare($this->width, $this->height) == 0) { } if($this->compare($this->width, $this->height) < 0) { } if($this->compare($this->width, $this->height) > 0) { }
thanks
for value returned method compare(), have 3 posibilities, have 2 conditions , can obmit one:
$recievedvalue = $this->compare($this->width, $this->height); if( $recievedvalue == 0 ) { // value equal 0 } else if( $recievedvalue < 0 ) { // value less 0 } else { // value more 0 }
Comments
Post a Comment