PHP Tutorial: If...else...else if Statements

If...else...else if Statements

PHP if else is used to verify the conditions if condition satisfied then the code inside he if executed otherwise inside the else. There are various ways to use if statement in PHP.

  • if
  • if-else
  • if-else-if
  • nested if

PHP If Statement:-

PHP if statement is executed if condition is true.
Syntax

<?php

if(condition){
//code to be executed
}

?>

Example

<?php

$num=55;

if($num<70){

echo "$num is less than 70";

}

?>

Output:
55 is less than 70

 

PHP If-else Statement:-

PHP if-else statement is executed whether you want to check the true false condition, if condition returns true in if otherwise else.
Syntax

<?php

if(condition){

//code to be executed if true

}else{

//code to be executed if false

}

?>

Example

<?php

$num=16;
if($num%2==0){

echo "$num is even number";

}else{

echo "$num is odd number";

}

?>

Output:
16    is even number
Note: if-else-if AND nested if not found simple program

Nested If Statement:

In nested if, we can use if inside and if:

Syntax:

<?php

If(condition)
{
      If(other condition)
      {
            //Your code
      }
}

?>

Example:

<?php

$a = 10;
$b = 12;
If($a<20)
{
           If($b>5)
         { 
                 Echo "$a is less than 20 and $b is greater than 5"
         }
}

?>

Next Topic