PHP If Else

PHP if else statement is used to test condition. There are various ways to use if statement in PHP.

PHP If Statement

PHP if statement allows conditional execution of code. It is executed if condition is true.

If statement is used to executes the block of code exist inside the if statement only if the specified condition is true.

Syntax

  1. if(condition){  
  2. //code to be executed  
  3. }

Flowchart

php if statement flowchart

Example

  1. <?php  
  2. $num=12;  
  3. if($num<100){  
  4. echo "$num is less than 100";  
  5. }  
  6. ?>  

Output:

12 is less than 100

PHP If-else Statement

PHP if-else statement is executed whether condition is true or false.

If-else statement is slightly different from if statement. It executes one block of code if the specified condition is true and another block of code if the condition is false.

Syntax

  1. if(condition){  
  2. //code to be executed if true  
  3. }else{  
  4. //code to be executed if false  
  5. }  

Flowchart

php if-else statement flowchart

Example

  

  1. <?php  
  2. $num=12;  
  3. if($num%2==0){  
  4. echo "$num is even number";  
  5. }else{  
  6. echo "$num is odd number";  
  7. }  
  8. ?>  

Output:

12 is even number

PHP If-else-if Statement

The PHP if-else-if is a special statement used to combine multiple if?.else statements. So, we can check multiple conditions using this statement.

Syntax

  1. if (condition1){    
  2. //code to be executed if condition1 is true    
  3. elseif (condition2){      
  4. //code to be executed if condition2 is true    
  5. elseif (condition3){      
  6. //code to be executed if condition3 is true    
  7. ....  
  8. }  else{    
  9. //code to be executed if all given conditions are false    
  10. }    

Flowchart

php if-else statement flowchart


Exmaple
  1. <?php  
  2.     $marks=69;      
  3.     if ($marks<33){    
  4.         echo "fail";    
  5.     }    
  6.     else if ($marks>=34 && $marks<50) {    
  7.         echo "D grade";    
  8.     }    
  9.     else if ($marks>=50 && $marks<65) {    
  10.        echo "C grade";   
  11.     }    
  12.     else if ($marks>=65 && $marks<80) {    
  13.         echo "B grade";   
  14.     }    
  15.     else if ($marks>=80 && $marks<90) {    
  16.         echo "A grade";    
  17.     }  
  18.     else if ($marks>=90 && $marks<100) {    
  19.         echo "A+ grade";   
  20.     }  
  21.    else {    
  22.         echo "Invalid input";    
  23.     }    
  24. ?>  

Output:

B Grade

PHP nested if Statement

The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.

Syntax

  1. if (condition) {    
  2. //code to be executed if condition is true   
  3. if (condition) {    
  4. //code to be executed if condition is true    
  5. }    
  6. }   

Flowchart

php if-else statement flowchart

Example

  1. <?php  
  2.                $age = 23;  
  3.     $nationality = "Indian";  
  4.     //applying conditions on nationality and age  
  5.     if ($nationality == "Indian")  
  6.     {  
  7.         if ($age >= 18) {  
  8.             echo "Eligible to give vote";  
  9.         }  
  10.         else {    
  11.             echo "Not eligible to give vote";  
  12.         }  
  13.     }  
  14. ?>  

Output:

Eligible to give vote

PHP Switch Example

  1. <?php  
  2.               $a = 34; $b = 56; $c = 45;  
  3.     if ($a < $b) {  
  4.         if ($a < $c) {  
  5.             echo "$a is smaller than $b and $c";  
  6.         }  
  7.     }  
  8. ?>  

Output:

34 is smaller than 56 and 45