0

I have a variable $user_id (number value)

Want to check if $user_id falls in between the range of 1 - 1000 or 1001 - 2000 or 2001 - 3000 .... 99001 - 100000

  1. Is there a way to do this without writing 100 switch or if statements in PHP?
  2. When it finds the match, execute a code.

I know while and for loops are required for this. But not able to code it properly.

Jestin
  • 111
  • 6
  • 3
    "_Is there a way to do this without writing 100 switch or if statements in PHP?_" That entirely depends on what you are planning to do with this range. If the code executed is completely different depending on the range, than switches/if-s might be your best bet. – Ivar Dec 03 '20 at 08:25
  • "_while and for loops are required for this_" -- incorrect, you should certainly not use a loop since the range can be determined through a one-time mathematical expression. – mickmackusa Dec 03 '20 at 21:06

3 Answers3

2

This the simplest way:

    $check = 0;
    $nextCheck = $check+1001;
    while ($check < 100001) {
    If ($user_id > $check && $user_id < $nextCheck) {
      // Code ...
      break;
    } else {
       $check+=1000;
       $nextCheck+=1000;
    }
    }
Jestin
  • 111
  • 6
Hida
  • 154
  • 10
  • 2
    "Try this" answers are low value on Stackoverflow because they do very little to educate/empower thousands of future researchers. – mickmackusa Dec 03 '20 at 08:56
  • I would not use this code in one of my projects. Iterating upto 1000 times is needless work. – mickmackusa Dec 03 '20 at 10:10
  • I think it's 100 loop not 1000. – Hida Dec 03 '20 at 10:51
  • And unfortunately I didn't ask you to use my code in all your project. – Hida Dec 03 '20 at 10:52
  • Yes. You are right, it is 100 needless iterations, my mistake. Your answer still lacks its educational explanation. A `for()` loop would have better readability. – mickmackusa Dec 03 '20 at 11:10
  • Right, I should use for() loop in the code above. It's will be more practice. I agree with that. – Hida Dec 03 '20 at 15:08
  • @Hida : Your code is something that I was looking for. But It stops functioning when $user_id is > 1000. But I managed with a minor tweak. I Added "$check+=1000; $nextCheck+=1000; " in place of "$check+1000;" – Jestin Dec 03 '20 at 18:45
2

Do not generate an array. Do not use a loop. Use math to determine the upper limit of the range, then subtract 999 from that number -- done.

*my snippet assumes we are only dealing with positive values between 1 and 100000.

Code: (Demo)

$tests = [1, 999, 1000, 50001, 100000, 2999];

foreach ($tests as $test) {
    $upper = intval(($test - 1) / 1000) * 1000 + 1000;
    echo "$test is between " . ($upper - 999) . " and $upper\n";
}

Output:

1 is between 1 and 1000
999 is between 1 and 1000
1000 is between 1 and 1000
50001 is between 50001 and 51000
100000 is between 99001 and 100000
2999 is between 2001 and 3000

Formula Breakdown:

intval(            #3) remove decimals from difference
    ($test - 1)    #1) subtract one
    / 1000         #2) divide by 1000
)
* 1000             #4) multiply integer by 1000
+ 1000             #5) add 1000
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @Jestin the answer that you accepted is not direct, it is an inefficient "guess and check" technique. Why would anyone not want to use my mathematical one-liner? You even state under the answer that it doesn't work -- why would you give it a green tick?! Now researchers will be biased toward a suboptimal answer. – mickmackusa Dec 03 '20 at 19:47
  • This should be marked as best answer – aurawindsurfing Apr 14 '23 at 11:20
0

You can just divide the number by 1000 since you have a 1000 interval range in a consecutive way.

The quotient * 1000 + 1 is your start value for the interval with an exceptional corner case of a number divisible by 1000, which would just be the border end for an interval.

<?php

$tests = [1,999,1000,50001,100000,2999];

foreach($tests as $test_case){
    $quotient = intval($test_case / 1000);
    if($test_case % 1000 === 0){
        $start = $test_case - 1000 + 1;
        echo "$test_case : Range: ($start - $test_case)",PHP_EOL;
    }else{
        $start = $quotient * 1000 + 1;
        $end = ($quotient + 1) * 1000;
        echo "$test_case : Range: ($start - $end)",PHP_EOL;
    }
}

Output:

1 : Range: (1 - 1000)
999 : Range: (1 - 1000)
1000 : Range: (1 - 1000)
50001 : Range: (50001 - 51000)
100000 : Range: (99001 - 100000)
2999 : Range: (2001 - 3000)

Demo: https://3v4l.org/apbqV

nice_dev
  • 17,053
  • 2
  • 21
  • 35