Glynne Turner
Glynne Turner

Reputation: 149

if number is a decimal between 2 numbers (php)

I'm writings a PHP script that calculates an average of a number.

EXAMPLE:

$rating = 7;
$votes = 3;

$AvgRating = number_format($rating / $votes,1); 

The return $AvgRating for this would be 2.3 (to 1 decimal place).

is it possible to say.....

if ($AvgRating > 2 && $AvgRating < 3){   
   $display = 'between';
}
echo $display;

I have tried but it does not work, I have tried google but don't know exactly what I need to look for.

Upvotes: 0

Views: 724

Answers (3)

David Myers
David Myers

Reputation: 411

This code evaluates correctly...

<?php
    $rating = 7;
    $votes = 3;

    $AvgRating = number_format($rating / $votes,1); 

    if ($AvgRating > 2 && $AvgRating < 3){   
       $display = 'between';
    }
    echo $display;
?>

Upvotes: 1

Kevin
Kevin

Reputation: 6691

If your question is whether the condition inside the if statement is correct, yes it is.

However in your case, the if condition fails since the value of $AvgRating (7/3) is less than 7. So you probably might be checking whether $AvgRating is greater than 2.

Upvotes: 0

Pugazhenthi
Pugazhenthi

Reputation: 90

Hi there is some mistakes $AvgRating retunrs 2.3 not 7.3

So you have to check with this

$rating =7; $votes=3;

$AvgRating = number_format($rating / $votes,1);

if ($AvgRating > 2 && $AvgRating < 3.0){

   echo $AvgRating; 
} 

Upvotes: 0

Related Questions