Control Statements in PHP| If Else in PHP | Nested If Else in PHP | Switch in PHP

Array operators in PHP | How to use array operator in PHP

Control statements are conditional statements that will run your statements if the condition is correct. The statement inside the conditional block will not execute until the condition is satisfied. Following are some control statements:

  • PHP if statements
  • PHP if-else statement
  • PHP if-else-if statement
  • PHP nested if statement
  • PHP switch statements

PHP if statements

PHP if statements check if the condition inside the block of statements is true or not. The code will only run if the given condition is true, otherwise, it will terminate.

Check this example

<?php 
$val = 99; 
if ($val < 100) { 
echo "$ num is less than 100"; 
} 
?>

The output will be:

num is less than 100

PHP if-else statement

In this we define two statements to check if one does not run, the other statement should run. PHP if-else statements do not terminate the code instead run the code which is written in the else part.

Check this example

<?php 
$val = 109; 
if ($val < 100) { 
 echo "$val is less than 100"; 
} 
else 
 echo "$val is greater than 100" ;
?>

The output will be:

109 is greater than 100.

PHP if-elseif-if statements

When the user has to check more than 2 conditions, then this statement is used.

Check this example:

<?php
$n = 50;
if ($n < 60) {
    echo "my name is dev";
} elseif ($n < "40") {
    echo "my name is anuj";
} else {
    echo "i don't have a name ";
}
?>

The output will be:

My name is dev.

PHP nested if statement

This statement runs the ‘if-else statement in the ‘if’ statement.

Check this example:

<?php 
$age = 20;
if ($age <= 40) {
    if ($age <= 15) {
        echo "child";
    } else {
        echo "young";
    }
} else {
    echo "old";
} ?>

The output will be:

young

PHP switch statements

Switch statements print out the statement from available choices. In such statements user does not give any condition rather gives multiple statements to choose from them.

Syntax of switch statement:

switch(expression) { 
  case value1: 
    //code to be executed
     break; 
  case value2: 
    //code to be executed
    break; 
  default: 
   code to be executed if all cases are not matched; 
}

Rules for switch statement

  • Switch statements are used with the ‘case value’ and ‘break’ parameters. You can define n number of case values.
  • ‘Break’ is used to jump to other case values if one case value does not match with the expression.
  • At the end of the last case value, the user has to define the ‘default’ statement. If any of the cases does not match with the ‘expression’ then the ‘default’ statement runs.
  • Nesting of switch statements is allowed, but it makes the program more complex and less readable.
  • You can use a semicolon (;) instead of a colon (:). It will not generate any error.

Check this example:

<?php $num = 8;
switch ($num) {
    case "7":
        echo "value is 7";
        break;
    case "8":
        echo " value is 8";
        break;
    default:
        echo "out of range";
        break;
} ?>

The output will be:

value is 8

The PHP switch statement is fall-through.

It means it will execute all statements after getting the first match if a break statement is not found.

Check this example:

<?php $num = 8;
switch ($num) {
    case "7":
        echo "value is 7";
        echo "<br>";
    case "8":
        echo "value is 8";
        echo "<br>";
    case "9":
        echo "value is 9";
        echo "<br>";
    default:
        echo "out of range";
        break;
}

The output will be:

value is 8 value is 9 out of range

Control Statements in PHP| If Else in PHP | Nested If Else in PHP | Switch in PHP
Show Buttons
Hide Buttons