PHP - Using if statement to check value range

Introduction

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

It uses the && logical operator:

Demo

<?php

     $widgets = 23;/*from   w ww.j  a  v  a  2  s  .c  o m*/

     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