Php If else basics

if else statements are very basic blocks to check condition(boolean expression) to execute appropriate block.

IF ELSE STATEMENTS :

SYNTAX:

<?php
		if (booleanExpression) {
		
		  // if booleanExpression is True this code will execute
		  your code here….
		  
		} else {
		
		  // if booleanExpression is False this code will execute
		  your code here….
		  
		}
?>	

Example 1 :

In below example, if block will be executed if x is greater than 11. Otherwise, the else block will be executed.

<?php
        
        $x = 10;
        if ($x > 6) {
		
             print "x > 6 is true, if block  execute. "
        }
        else{
		
             print "x > 6 is false then else block  execute. "
        }             
?>

------------ ****** ------------
OUTPUT:
        x > 6 is true, if block execute.

Example 2 :

In below example, if block will be executed if x is greater than 11. Otherwise, the else block will be executed.

<?php

       $x = 9;
       if ($x > 11){
	   
            print "x > 11 is true, if block  will execute. ";
       }
       else{
	   
            print "x > 11 is false then else block will execute. ";
       }
?>

------------ ****** ------------
OUTPUT:
        x > 11 is false so else block will execute.
		

Example 3 :

If there is only one line code inside an if or else block, the curly braces ({) are optional.

<?php

        $x = 9;
		
        if ($x > 11) 
            print "x > 11 is true, if block  will execute. "
        else
            print "x > 11 is false then else block will execute. "
			
?>

------------ ****** ------------
OUTPUT:
        x > 11 is false so else block will execute.
		

IF ELSE-IF ELSE STATEMENTS :

if there are many condition need to check then we can use ifelse block.

SYNTAX:

<?php

		if (booleanExp_1) {
	
		   // booleanExp_1 is True then this block code will execute.
	
		} else if (booleanExp_2) {
	
		  // booleanExp_2 is True then this block code will execute.
	
		}
	   else {
	   
		  // if all upper conditions will fail(False) then this block code will execute.
		}
?>		

Example 4:

If there are many conditions in else-if then which condition is true that block code will execute. .

<?php  
       
        $c = 3;
        if ($c == 1) {  // False
		
             print "one"
			 
        }else if ($c == 2) { // False
		
             print "two"
			 
        }else if ($c == 3) { // True
		
             print "three"
			 
        } else { 
		
             print "invalid"
        }
		
?>

------------ ****** ------------
OUTPUT:
        three
		

Example 5:

If there are many conditions then, if all conditions are false then always else block code will execute.

<?php         

         $c = 5;
         if ($c == 1) {  // False
		 
             print "one"
			 
         } else if ($c == 2) { // False
		 
              print "two"
			  
         } else if ($c == 3) { // False
		 
             print "three"
			 
         } else { 
		 
            print "All expressions are false, else block executed."
			
         }
 ?>
 
 ------------ ****** ------------
 OUTPUT:
         All expressions are false, else block executed.
		 
 

Use Of Operators :

See how to use && and || operators for if else expressions.

For && both conditions should be True then it will True.

For || any one conditions is True then it will True.

Example 6 :

Taking example for use of && operator.

<?php   
     
        $c = 5;
        if ($c == 1 && $c == 5) {  // False
		
             print "if block (c == 1 && c == 5)  is executed"
			 
         } else if ($c == 5 && $c < 10) { // True
		 
              print "elseif block (c == 5 && c < 10) is executed"
			  
         } else { 
		 
            // This upper both expressions are False then this block code execute. 
            print "else block  is executed"
			
         }
?>

------------ ****** ------------
OUTPUT:
        elseif block (c == 5 && c < 10) is executed
		

Example 7:

Taking example for use of || operator.

<?php   
      
        $c = 5;
        if ($c == 1 || $c == 5) {  // True
		
              print "if block (c == 1 || c == 5)  is executed"
			  
        } else if ($c == 6 || $c < 10) { // False
		
             print "elseif block (c == 6 || c < 10) is executed"
			 
        } else {
		 
           // This upper both expressions are False then this block code execute. 
           print "else block  is executed"
		   
        }
		
 ?>
 
------------ ****** ------------
OUTPUT:
         if block (c == 1 || c == 5) is executed
		 
 

 

SWITCH STATEMENT:

Internallly Switch statement working is same as elseif blocks.

switch accept condition and contain many case statements, Whenever case statement is true then that

case block code will execute, when found break then case statement will break and execution will out from switch statement.

SYNTAX:

<?php 

	switch (condition) {

	     case value_1 :
               // You Code Here….
             break;
         case value_2 :
              // You Code Here….
             break;
          .
          .
          .
          .
         case value_n :
             // You Code Here….
           break;
         default:
            // You Code Here….
      }
	  
 ?>

Example 8:

In below example, if block will be executed if x is greater than 11.

Otherwise, the else block will be executed.

<?php 
       
       $a = 1;
         switch ($a) {
		 
            case 1 :
               print "First case.";
               break;
           case 2 :
               print "Second case.";
               break;
           case 3 :
               print "Third case.";
               break;
           default:  // if none of the case 
               print "Default."
			   
         }  // Switch close
		 
?>

------------ ****** ------------
OUTPUT:
         First case.
		 

Example 9:

if we will not take break in case statement, then switch will executed for other case until found break or default statement.

<?php
        
       $a = 2;
       switch ($a) {
	   
           case 1 :
               print "First case.";
               break;
           case 2 :
               print "Second case.";
           case 3 :
               print "Third case.";
                 default:  // if none of the case 
               print "Default.";
        }  // Switch close
			 
?>

------------ ****** ------------
OUTPUT:
       Second case.
	   Third case.
	   Default.
	   

Example 10:

We can use nested Switch statements.

<?php  
       
        $a = 1;
        $b = 2;
		
        switch ($a) {
		
             case 1 :
			   
					print "Outer First case.";
					// When case 1 is true then inner switch will call
					// Inner switch statement start
					
						switch ($b) {
						
								case 1 :
									print "Inner First case.";
									break;
								case 2 :
									print "Inner Second case.";
									break;   
								default:  // if none of the case 
									print "Inner Default.";
								 
							}  // Switch close
							
					 // Inner switch statement end
					 
                break;
            case 2 :
			
               print "Outer Second case.";
                break;
                 
            default:  // if none of the case 
			
                print "Outer Default.";
             
        }  // Switch close
		
?>

------------ ****** ------------
OUTPUT:
        Outer First case.
		Inner Second case.