How to Check If an Integer Is Within a Range of Numbers in PHP

How to check if an integer is within a range of numbers in PHP?

The expression:

 ($min <= $value) && ($value <= $max)

will be true if $value is between $min and $max, inclusively

See the PHP docs for more on comparison operators

How to check if an integer is within a range?

Tested your 3 ways with a 1000000-times-loop.

t1_test1: ($val >= $min && $val <= $max): 0.3823 ms

t2_test2: (in_array($val, range($min, $max)): 9.3301 ms

t3_test3: (max(min($var, $max), $min) == $val): 0.7272 ms

T1 was fastest, it was basicly this:

function t1($val, $min, $max) {
return ($val >= $min && $val <= $max);
}

How to check if a number (float or integer) is within a range (0 - 100)

No need for regex:

if (is_numeric($val) && $val > 0 && $val <= 100)
{
echo '$val is number (int or float) between 0 and 100';
}

Demo

Update

It turns out you're getting the numeric values from a string. In that case, it would be best to extract all of them using a regex, something like:

if (preg_match_all('/\d+\.?\d*/', $string, $allNumbers))
{
$valid = [];
foreach ($allNumbers[0] as $num)
{
if ($num > 0 && $num <= 100)
$valid[] = $num;
}
}

You can leave out the is_numeric check, because the matched strings are guaranteed to be numeric anyway...

How to check if a number falls in a given range

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

PHP: How to know if number is in a interval of int?

Php does not have it, but you can create it like this example.


function in($number,$min,$max){
if($number >= $min and $number <= $max){
return true;
}
return false;
}

Checking the Range of a Number in PHP

Get rid of the % from your $percent_completed variable. It makes it as string which will give you different results than when comparing as a integer (for numbers).

$percent_completed = 100;                            

if ($percent_completed <= 20) {
$color = 'danger';
} elseif ($percent_completed < 40) {
$color = 'warning';
} elseif ($percent_completed < 60) {
$color = 'info';
} elseif ($percent_completed < 80) {
$color = 'primary';
} elseif ($percent_completed < 100) {
$color = 'default';
} else {
$color = 'success';
}

echo $color;

PHP: How can I determine if a variable has a value that is between two distinct constant values?

if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))

PHP: How to check if a number is between collection of two numbers

Due to comments above, it should works as expected:

$count = 34; // any number
$per_page = 10; // it's fixed number, but...
$num_page = ceil($count / $per_page); // returns 4
$low_limit = ($num_page - 1) * $per_page; // returns 30
$up_limit = $num_page * $per_page; // returns 40

Between record number 30 and record number 40 are 11 records, not 10.

There are more ways how to fix that:

1. compare < ... <=: $low_limit < $count <= $up_limit
2. compare <= ... <: $low_limit <= $count < $up_limit
3. set limits 1-10, 11-20, 21-30, etc. (simply +1 to $low_limit on 4th line above)

4. set limits 0-9, 10-19, 20-29, etc. (simply -1 to $up_limit on 5th line above)

How to check if the set of numbers contains a certain number in PHP?

You have to be able check the numbers in the set programmatically, like so:

<select name="">
<?php
$set_of_numbers = [1, 2, 4, 5, 9];

for ($i = 1; $i <= 12; $i++) {
if (!in_array($i, $set_of_numbers)) {
echo '<option value='.$i.'>'.$i.'</option>';
}
}
?>
</select>

If your set of numbers is and can only be a string, then you would probably go with something like this:

$set_of_numbers = "1,2,4,5,9";
$set_of_numbers = explode(',', $set_of_numbers); // This makes an array of the numbers (note, that the numbers will be STILL stored as strings)

If you want to be able to compare the numbers as integers, the solution would be:

$set_of_numbers = "1,2,4,5,9";
$set_of_numbers = json_decode('[' . $set_of_numbers . ']'); // This creates a valid JSON that can be decoded and all of the numbers WILL be stored as integers

Hope, you've got this :)

Faster way to check if integer is in any of ranges?

faster and shorter - no*, but you can make it more flexible and elegant

function find_range($n, $ranges) {
foreach($ranges as $key => $range)
if($n >= $range[0] && $n <= $range[1])
return $key;
return false;
}

$ages = array(
'baby' => array(0, 1),
'child' => array(2, 13),
'teen' => array(14, 19),
'adult' => array(20, 59),
'senior' => array(60, 100)
);

var_dump(find_range(20, $ages));

(* assuming ranges are arbitrary. If we know more about ranges, for example, that they are sorted, or always intersect, or follow some formula, we can find a better algorithm).



Related Topics



Leave a reply



Submit