how would i find out if an number is in between certain values.

ex
number
33

Set values
0-10
10-20
20-30
30-40

output
30-30

thanks in advance

Member Avatar for diafol

Your example makes no sense. Do you mean output = 30-40?
In addition, you don't want to have a max value in a set and an identical min value in another set. If you chose 20: 10-20 or 20-30?

0-9
10-19
20-29
...

would be more appropriate?

you could use integer division to get the lower value then add ten to get the upper
33 / 10 * 10 = 30
30 + 10 = 40

Your example makes no sense. Do you mean output = 30-40?
In addition, you don't want to have a max value in a set and an identical min value in another set. If you chose 20: 10-20 or 20-30?

0-9
10-19
20-29
...

would be more appropriate?

yes you are right i will have to change things to make it more like that. so based upon that new info. how would i go about finding what group it goes into

Hey. You'll need and if statement that takes the input value and checks to see what range it's in. Pretty simple really...

if ($input >= 0 and $input < 10)
{
    echo "Input = 0-9";
}
elseif ($input >= 10 and $input < 20)
{
    echo "Input = 10-20";
}
elseif ()
{
    // etc
}

Hmmm. I thought it was going to be harder. Learn something new every day. Thanks again

Member Avatar for diafol

This is all well and good if you have preselected sets (0-9,10-19 etc). Are you constraining your value to say less than 100? Or could it be any value? If so, you need something a little more subtle. In addition do you need dynamic ranges, e.g. 0-19,20-39 or can you start on a different integer, e.g. 4-13,...

I can imagine quite a few ways to do this. You could go along with the multiple if..elseif..else routine, or do a switch (preferable to previous), or a loop.

This is a rough and ready idea - not tested - I know that it's sloppy and numbers for cutoff aren't exact...

$start_int = 0; //start at this number
$range_nos = 10; //no. of numbers in range (0 - 9 = 10);
$cutoff = 1000; //do not go past this number - avoid infinite loop
  
//test to see if $value is less that $cutoff and more than $start_int

while($start_int < $cutoff){
 
 $top_no = $start_int + $range_nos;
 $max = $top_no - 1;

 if($value >= $start_int && $value < $top_no){
   $output = "The value $value is in set $start_int TO $max";
   break; 
 }
 $start_int = $top_no;
}

echo $output;

This is slower than the switch/multiple if (*I think*), but it's dynamic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.