phpicoder Nov 2, 2021 php

Hi guys, in this tutorial we will learn PHP if else elseif Statements.

When writing programs / scripts, there will be situations where certain conditions are met only if you want to run a specific statement. In such situations we use conditional statements.

PHP we have the following 3 conditional statements:

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

if statement:

if statement condition is a true executes code 

Syntax:
if(condition){
    //*********//
}
Example:
<?php
  $date = '1990';

  if ($date == '1950') {
    echo "Happy Birthday Johan";
  }

  if ($date == '1990') {
    echo "Happy Birthday Andy";
  }
?>
Output:
Happy Birthday Andy

if...else Statement 

if...else Statement condition is s true executes if code or condition is s false executes else part

Syntax:
if(condition){
    //*********//
}else{
    //*********//
}
Example: 
<?php
  $number = 10;

  if($number%2 == 0){
    echo "$number is a even number";
  }else{
    echo "$number is a odd number";
  } 
?>
Output:-
10 is a even number

if...elseif....else Statement:

if have a more than two conditions then use if...elseif....else Statement, if one condition is a false check elseif  conditions and all conditions is a false executes else part.

Syntax:
if(condition){
    //*********//
}elseif(condition){
    //*********//
}else{
    //*********//
}
Example:
<?php  
  $per=69;

  if ($per >= 70){
    echo "Distinction";
  }else if ($per >= 60){
    echo "First Class";
  }else if ($per >= 50){
    echo "Secund Class";
  }else if ($per >= 35){
    echo "Pass";
  }else{
    echo "Failed";
  }    
?>
Output:
First Class

I hope this tutorial help for you.