Demystifying Conditions

Conditions enable your code to make decisions based on various criteria. Here are some common examples:

1. if...else

This checks a single condition:

    $age = 22;
    if ($age >= 18) {
        echo "You are eligible to vote.";
    } else {
        echo "You are not yet eligible to vote.";
    }
    

2. if...elseif...else

This checks multiple conditions sequentially, executing the first matching block:

    $grade = 85;
    if ($grade >= 90) {
        echo "Excellent work!";
    } elseif ($grade >= 80) {
        echo "Great job!";
    } else {
        echo "Keep practicing!";
    }
    

3. Switch/Case

This compares a value against multiple possibilities:

    $day = "Sunday";
    switch ($day) {
        case "Monday":
            echo "Start of the week!";
            break;
        case "Friday":
            echo "TGIF!";
            break;
        default:
            echo "It's a weekday.";
    }
    

Remember, conditions enhance your code's flexibility and allow for dynamic behavior based on different scenarios.