Account
Categories

If-elseif-else Statement


To check the condition in PHP, if the condition is true, the code inside the if braces will execute.

If the condition is not true, it goes to the next and checks the condition.

If that condition is true, then the code inside the elseif braces runs.

If no condition is true, then the code inside the else braces runs.

Now you will clearly understand the code logic with the help of the flowchart.

If-elseif-else Statement Flowchart :

First step: → You will write the program
           |
Second step: → Now you will check the condition (true/false)

If you find "True", then you will go to the "if block"
          |
       Execute Code
If the first condition is "False", then you will check the 
  else-if condition.
          |
      If else-if is True → Execute Code.
          |
If else-if is also False, then you will go to the "else block"
          |
       Execute Code
          |
       End the program
Syntax:
if (condition1) {
    // If condition1 is true then code executes.
} elseif (condition2) {
    // If condition2 is true then code executes.
} else {
    // If no conditions are true then code executes.
}
Example:
<?php
$number = 15;

if ($number > 20) {
    echo "Number is bigger than 20";
} elseif ($number > 10) {
    echo "Number is bigger than 10 but less than or equal to 20";
} else {
    echo "Number is 10 or less";
}
?>
Output:
Number is bigger than 10 but less than or equal to 20