简体   繁体   English

如何检查数字是否在给定范围内

[英]How to check if a number falls in a given range

I have a variable $user_id (number value)我有一个变量$user_id (数值)

Want to check if $user_id falls in between the range of 1 - 1000 or 1001 - 2000 or 2001 - 3000.... 99001 - 100000想要检查$user_id是否落在 1 - 1000 或 1001 - 2000 或 2001 - 3000 的范围内.... 99001 - 100000

  1. Is there a way to do this without writing 100 switch or if statements in PHP?有没有办法不用在 PHP 中写 100 switchif语句就可以做到这一点?
  2. When it finds the match, execute a code.当它找到匹配时,执行代码。

I know while and for loops are required for this.我知道这需要 while 和 for 循环。 But not able to code it properly.但无法正确编码。

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;
    }
    }

You can just divide the number by 1000 since you have a 1000 interval range in a consecutive way.您可以将该数字除以1000 ,因为您有一个连续的 1000 间隔范围。

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. quotient * 1000 + 1是间隔的起始值,其中一个特殊的极端情况是一个数字可以被 1000 整除,这只是一个间隔的边界结束。

<?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: 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演示: https://3v4l.org/apbqV

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.使用数学确定范围的上限,然后从该数字中减去999完成。

*my snippet assumes we are only dealing with positive values between 1 and 100000. *我的代码片段假设我们只处理 1 到 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: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM