Getting Started With Conditional Statements 
( If, If Else, Elseif, Switch)

Getting Started With Conditional Statements ( If, If Else, Elseif, Switch)

Just like the name implies, conditional statements are used to perform various actions depending on different conditions.

The IF Statement

The if statement is used to perform an action if a condition is true while if not true, it ignores.

Below is the syntax

if (condition) { code to be executed if condition is true; }

Sample Code:-

<?php
$a = 10
$b = 8
if ($a > $b){
  echo "a is bigger than b";}
?>

The above code would display a is bigger than b if it is bigger, else it would ignore.

The IF ELSE Statement

IF ELSE provides a code to be run if a statement is true and another if the statement is false.

Below is the syntax

if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }

Sample Code:=

<?php
$a = 10
$b = 8
if ($a > $b){
  echo "a is bigger than b";}
else{
  echo "a is not bigger than b"  }
?>

The above code would display a is bigger than b but if we change the values, it becomes different

<?php
$a = 5
$b = 8
if ($a > $b){
  echo "a is bigger than b";}
else{
  echo "a is not bigger than b"  }
?>

The above code would print a is not bigger than b

The IF ELSEIF ELSE Statement

IF ELSEIF ELSE provides different codes to be run for more than two options.

Below is the syntax

if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if first condition is false and this condition is true; } else { code to be executed if all conditions are false; }

Sample Code:-

<?php
$a = 10
$b = 8
if ($a > $b){
  echo "a is bigger than b";}
elseif($a < $b) {
  echo "a is lesser than b"  }
else{
     echo "a is equal to b" }
?>

The above code would print a is bigger when a is bigger but if a isn't bigger it checks if it is smaller if it isn't smaller also, it prints a is equal to b.

Switch Statement

The SWITCH statement is used to select one of many blocks of codes to be executed.

Below is the syntax

switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }

Sample Code:=

<?php
$favcolor = "white";

switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
    break;
  case "blue":
    echo "Your favorite color is white";
    break;
  case "green":
    echo "Your favorite color is black";
    break;
  default:
    echo "Your favorite color is neither red, white, nor black!";
}
?>

The value of the expression is compared with the value of each case, once a match is detected, the block of code, in that case, is executed. The break is used to stop it from running into the next case automatically, while the default is for when the statement doesn't match with either of the cases.

I hope you enjoy reading this article.