Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 614
Gabriel Meono Avatar asked Apr 13 '11 23:04

Gabriel Meono


People also ask

How to check between condition in PHP?

if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) { // A value between 1 to 10, or 20 to 40. }

How can I check if two values are same in PHP?

The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same.


2 Answers

if (($value > 1 && $value < 10) || ($value > 20 && $value < 40)) 
like image 118
Daniel A. White Avatar answered Sep 25 '22 13:09

Daniel A. White


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 
like image 42
Ryan Avatar answered Sep 21 '22 13:09

Ryan