I am trying to find whether a variable that I am calculating falls within a 20% range of a predefined number.
For example: predefined number is 8 (20% is 1.6) so the number would need to fall between 6.4 and 9.6.
What would be the best way to do this via PHP please?
//——————- APOLOGIES FOUND A WORKING ANSWER
Apologies worked this out by thinking about it!
Here is the code for others:
// Calculating the variables and defining the top and bottom ($read = 8 for example)
$calspeed = $read * 0.2;
$topspeed = $read + $calspeed;
$btmspeed = $read - $calspeed;
// $avg is taken from a sql query drawn from database
if ($avg > $btmspeed && $avg < $topspeed){
echo "this is an acceptable reading speed";
}else{
echo "wrong";
}
This has the benefit of supporting integers and allows you to coerce the values into something similar for the more complicated data formats.