- PHP

Switch and Case for Bigger Than X in PHP

Sometimes we want to perform some action if the number is bigger than the other number. When we want to do many comparisons like this, we can use a switch statement.

To do this, use the true inside the switch and then compare the numbers inside cases.

Here is an example:

If you want to use a similar logic, note that you should place your cases in descending order.

<?php

$number = 60;

switch (true) {
    case $number > 100:
        echo 'awesome';
        break;

    case $number > 50:
        echo 'good';
        break;

    case $number > 20:
        echo 'okay';
        break;
    
    default:
        echo 'too low';
        break;
}

Output: good