PHP Conditional Statements

John Wachira Jan 30, 2023
  1. the if Statement in PHP
  2. the if...else Statement in PHP
  3. the if...elseif..else Statement in PHP
  4. Nested if Statement in PHP
PHP Conditional Statements

This article will discuss the different conditional statements we can use in PHP. We can instruct PHP to act based on our conditions.

Here is a list of the conditional statements available in PHP.

  1. if statement
  2. if...else statement
  3. if...elseif..else statement
  4. switch statement

the if Statement in PHP

PHP will execute the code block in the if statement if the set condition is true.

Syntax:

if(condition){
  //Code
}

Let’s look at an example.

<?php
$t=12;
if($t<50){
echo "$t is less than 50";
}
?>

Output:

12 is less than 50

the if...else Statement in PHP

The if...else statement allows us to execute a block of code if the set condition is true and another block of code if the condition is false.

Syntax:

if(condition){
  //Code
}else{
  //Code
}

Example:

We can use the if...else statement to group the even and odd numbers.

<?php
$t=12;
if($t%2==0){
echo "$t is even number";
}else{
echo "$t is odd number";
}
?>

Output:

12 is even number

the if...elseif..else Statement in PHP

We use it to check more than two conditions when executing a code.

Syntax:

if(condition 1){
//Run code if condition is True;
}elseif(condition 2){
//Run Code if condition 1 is False and condition 2 is True;
}else{
//Run code if all conditions are false;
}

Let’s look at a practical example.

We can create a grading system using the if...elseif..else statement.

<?php
    $m=69;
    if ($m<33){
        echo "fail";
    }
    else if ($m>=34 && $m<50) {
        echo "D grade";
    }
    else if ($m>=50 && $m<65) {
       echo "C grade";
    }
    else if ($m>=65 && $m<80) {
        echo "B grade";
    }
    else if ($m>=80 && $m<90) {
        echo "A grade";
    }
    else if ($m>=90 && $m<100) {
        echo "A+ grade";
    }
   else {
        echo "Invalid input";
    }
?>

Output:

B grade

Nested if Statement in PHP

A nested if statement has an if block inside another if block. The outer statement must be true for the inner statement to execute.

Syntax:

if(condition){
  //code
  if(condition){
    //code
  }
}

Let’s look at a practical example.

We can use a nested statement to create a simple voting eligibility test.

Example:

<?php
    $a= 23;
    $nationality = "Dutch";
    //applying conditions on nationality and age
    if ($nationality == "Dutch")
    {
        if ($a >= 18) {
            echo "Eligible to vote";
        }
        else {
            echo "Not eligible to vote";
        }
    }
?>

Output:

Eligible to vote

It is not always advisable to nest deeply. The logic becomes hard to follow.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - PHP Statement