PHP If-Else Condition

The if statement is used to execute code only when a condition is true. The else block runs when the condition is false.

Example:
<?php
    $age = 20;

    if ($age >= 18) {
        echo "You are an adult.";
    } else {
        echo "You are not an adult.";
    }
?>

👉 Explanation:

  • if checks the condition and executes the block if true.
  • else executes only when the if condition is false.
  • Useful for making decisions based on user input or variables.

PHP If - Elseif - Else

The elseif statement allows checking multiple conditions one by one.

Example:
<?php
    $marks = 75;

    if ($marks >= 90) {
        echo "Grade A";
    } elseif ($marks >= 75) {
        echo "Grade B";
    } elseif ($marks >= 50) {
        echo "Grade C";
    } else {
        echo "Fail";
    }
?>

👉 Explanation:

  • if checks the first condition.
  • elseif checks additional conditions when the above ones are false.
  • else runs when all conditions are false.
  • Used when multiple outcomes are possible — such as grading, pricing, or decision trees.
PHP Tutorial