NaOH
NaOH

Reputation: 459

Checking if number is within range not working in PHP?

I'm using this code:

if (($height/$width) > 0.5 && ($height/$width) < 0.8) {
  // do stuff
}

For a particular image, $height/$width evaluates to 0.66543438077634 (1080/1623). And yet this appears to be evaluating as false. Can anyone suggest why?

Upvotes: 0

Views: 77

Answers (2)

GautamD31
GautamD31

Reputation: 28763

Try it like

$div = (float)($height/$width);
if ( $div > 0.5 && $div < 0.8) {
  // do stuff
}

Or you can use

$div = (float)($height)/(float)($width);

Upvotes: 0

Adam
Adam

Reputation: 1371

Propably You have some problem someware else because this code works fine:

<?
    $height = 1080;
    $width = 1623;
    if (($height/$width) > 0.5 && ($height/$width) < 0.8) {
        echo 'IT WORKS';
    }
    var_dump(($height/$width));
?>

SEE WORKING CODE!

Upvotes: 2

Related Questions