How to check if an integer is within a range?

前端 未结 8 781
轻奢々
轻奢々 2020-11-28 07:35

Is there a way to test a range without doing this redundant code:

if ($int>$min && $int<$max)

?

Like a function:

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 07:58

    There is no builtin function, but you can easily achieve it by calling the functions min() and max() appropriately.

    // Limit integer between 1 and 100000
    $var = max(min($var, 100000), 1);
    

提交回复
热议问题