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

9

46

How can I determine using PHP code that, for example, I have a variable that has a value

  • between 1 and 10, or
  • between 20 and 40?
Mada answered 13/4, 2011 at 23:0 Comment(3)
No, I don't get the idea! You mean "pick a random number between 1 and 10"? Or "calculate the value half-way between 1 and 10"? Or something else?Ovaritis
Your question is not clear. Do want to get a random value between 1 to 10 or 20 to 40 or do you want to test whether some value is in these ranges? Or even something else?Bullis
I know, PHP could really do with the SQL equivalent of the BETWEEN statement. It's not hard to do a if(x>=1 && x<=10), but somehow a between might be neater.Henbit
O
90
if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))
Otten answered 13/4, 2011 at 23:3 Comment(3)
Leaves out the fact that the number could be 1 or 10, 20 or 40.Altheta
@Altheta got to love the ambiguity of English and off by one errorsOtten
there is no helper function to check a number between 2 numbers in PHP?Shrine
U
22

Do you mean like:

$val1 = rand( 1, 10 ); // gives one integer between 1 and 10
$val2 = rand( 20, 40 ) ; // gives one integer between 20 and 40

or perhaps:

$range = range( 1, 10 ); // gives array( 1, 2, ..., 10 );
$range2 = range( 20, 40 ); // gives array( 20, 21, ..., 40 );

or maybe:

$truth1 = $val >= 1 && $val <= 10; // true if 1 <= x <= 10
$truth2 = $val >= 20 && $val <= 40; // true if 20 <= x <= 40

suppose you wanted:

$in_range = ( $val > 1 && $val < 10 ) || ( $val > 20 && $val < 40 ); // true if 1 < x < 10 OR 20 < x < 40
Underpass answered 13/4, 2011 at 23:3 Comment(0)
P
10

You can do this:

if(in_array($value, range(1, 10)) || in_array($value, range(20, 40))) {
   # enter code here
}
Penneypenni answered 2/1, 2019 at 22:53 Comment(2)
This looks very clean but be aware that i.e. testing for a price being between one and million means creating an array with million items. I've just measured both ways with microtime() and this one seems to run around 2000x slower for such large range while also consuming a lot of memory.Doone
the solution looks wonderful, works AwfulMatti
N
7
if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) {
   // A value between 1 to 10, or 20 to 40.
}
Nationwide answered 13/4, 2011 at 23:3 Comment(0)
D
5

Sorry for the late answer, but this function allow you to do that.

  function int_between($value, $start, $end) {
    return in_array($value, range($start, $end));
  }

  // Example
  $value1 = 20;
  $value2 = 40;
  echo int_between(20, $value1, $value2) ? "true" : "false";
Danieldaniela answered 22/1, 2022 at 23:27 Comment(0)
P
4

Guessing from the tag 'operand' you want to check a value?

$myValue = 5;
$minValue = 1;
$maxValue = 10;

if ($myValue >= $minValue && $myValue <= $maxValue) { 
  //do something
}
Park answered 13/4, 2011 at 23:4 Comment(0)
G
1

This is a good solution.

function int_between($value, $min, $max) {
    return in_array($value, range($min, $max));
}

I think this one is faster than the above

function int_between2($value, $min, $max) {
    return $value == min($max, max($min, $value));
}

But, this is 3 times faster than others.

function int_between3($value, $min, $max) {
    return ($value >= $min && $value <= $max);
}
Gracie answered 20/3, 2023 at 23:33 Comment(0)
L
0

If you just want to check the value is in Range, use this:

   MIN_VALUE = 1;
   MAX_VALUE = 100;
   $customValue = min(MAX_VALUE,max(MIN_VALUE,$customValue)));
Lull answered 15/2, 2018 at 10:24 Comment(2)
This doesn´t check the value is between the range, it returns a new value that will be between the range.Cavin
Must be explained, given custom value, will return custom value itself if in rangeCysticercoid
L
0

Try This

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

This will return the value between 1 to 10 & 20 to 40.
Linus answered 27/9, 2018 at 7:8 Comment(1)
Leaves out the fact that the number could be 1 or 10, 20 or 40.Linus

© 2022 - 2024 — McMap. All rights reserved.