PHP - Using if statement to check value range


Here's another example that uses the >= (greater than or equal) and <= (less than or equal) comparison operators.

It uses the && logical operator:



     $widgets = 23;/*from   ww  w .  j  ava 2  s.c  om*/

     if ( $widgets  >= 10  &&  $widgets  <= 20 ) {
         echo "We have between 10 and 20 widgets.";

Here, if the value stored in $widgets is greater than or equal to 10, and it's also less than or equal to 20, the expression evaluates to true.

If either of the comparison operations evaluates to false , the overall expression also evaluates to false.

Related Exercise