45

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?
Dennis
  • 7,907
  • 11
  • 65
  • 115
Gabriel Meono
  • 990
  • 3
  • 18
  • 48
  • 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? – Oliver Charlesworth Apr 13 '11 at 23:02
  • 2
    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? – Felix Kling Apr 13 '11 at 23:02
  • 1
    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. – Justin Levene Nov 19 '20 at 10:51

9 Answers9

89
if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))
cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
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
Ryan
  • 925
  • 2
  • 6
  • 25
10

You can do this:

if(in_array($value, range(1, 10)) || in_array($value, range(20, 40))) {
   # enter code here
}
HFranco
  • 592
  • 6
  • 9
  • 6
    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. – Josef Habr Oct 14 '20 at 12:25
7
if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) {
   // A value between 1 to 10, or 20 to 40.
}
don
  • 1,497
  • 1
  • 13
  • 27
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";
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
}
Codecraft
  • 8,291
  • 4
  • 28
  • 45
2

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)));
0

Try This

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

This will return the value between 1 to 10 & 20 to 40.
kakarot
  • 3
  • 3
0

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);
}
S.B
  • 13,077
  • 10
  • 22
  • 49
ByGone
  • 1
  • 1