PHP if..else elseif Statements

  • 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.

In PHP we have the following 3 conditional statements:

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

If :

if statement condition is a true executes code ...

PHP Syntax

<?php
	if(condition){
    //*********//
}
?>

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
 $date = '1990';

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

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


</body>
</html>

Output :

Edit it yourself

Happy Birthday Andy


If...Else :

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

PHP Syntax

<?php
	if(condition){
      //*********//
  }else{
      //*********//
  }
?>

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
 $number = 10;

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


</body>
</html>

Output :

Edit it yourself

10 is a even number


I...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...

PHP Syntax

<?php
	if(condition){
    //*********//
  }elseif(condition){
      //*********//
  }else{
      //*********//
  }

?>

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?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";
  }    
?>


</body>
</html>

Output :

Edit it yourself

First Class