PHP conditional statements(if-else)

php conditional statements(if else, if elseif else) are used to control the execution of statements based on the conditions.

 php provides the four different type of conditional statements.such as

  • if   statement
  • if   . . . .  else   statement
  • if   . . . .  elseif  . . . . else   statement
  • switch . . case   statement

PHP if statement

the if statement is straight forward if the given condition is true, the statements within braces will be executed.otherwise, they are skipped.

syntax :

                if(condition){
                           
                                 statement__1 ;
                                                         code part only executed if condition is true
                                 statement__2 ;
                                                                               
                           }

condition can be any expression that evaluates to true or false.

note : if statements can be nested infinitely within other if statements.

       <?php
       
              $x = 30;  $y = 50;
              
              if( $x <= $y ){   echo  "the condition is true" ;
                                                             
                                     echo  "<br/> x value = {$x}"; 
                                }
                                                       
              if(false) echo "this condition is false" ;
              
              if(true)  echo "<br/>true is predefined keyword";
     
    ?>

you can have 'nested' if statements within a single if statement, using additional parenthesis.

  <?php
     
          if( 12 == 12 ){
                             
                                      echo "this is outer if block <br/>";
                                                     
                   if( 2<3 && 3 == 3){
                                                 
                                             if(true){
                                                                 
                                                      echo "this is inner inner if block <br/>";
                                                 }
                                                                          
                                                      echo "this is inner if block";  
                                            }
                                                                     
                                       }
      ?>

PHP if . . .  else statement

anytime when condition is true then if evaluates block is executed otherwise the else block execute.

the else clause is optional.

syntax:

                if(condition){
                      
                                                 if block   executed only if the condition is true
                                   
                              }else {
                                     
                                             else block  executed if the condition is false
                                        
                                    }

the else statement is only executed when the if condition evaluated to false.

    <?php    $x = 5 ;  $y = 6 ;
           
                                 if( $x >= $y ){   echo "this is if block";
                             
                                           }else{    echo "this is else block {$x} <br/>"; 
                                                                      
                                               }  
                                               
                                 if(true){ echo "you look this book"; 
                                 
                                          }else{                                                       
                                                     echo "yes i look" ;
                                                       
                                               } 
                                                                             
         ?>

PHP if  .. elseif  .. else statements

the php if  elseif  else statement use to multiple conditions tested in sequence.

syntax:

                if(condition__1){
                      
                                                      if block   executed only if the condition__1 is true
                                   
                              }
                
                elseif (condition__2){              
                
                                               elseif block   executed only if the condition__2 is true
                                                
                               }else {
                                     
                                           else block  executed if the all conditions are false
                                        
                                    }
<?php    $x = 20;
      
  if( $x <= 10 ){  echo  "x is less then 10 ";
               
                 } elseif( $x <= 15){  echo "x is less then 15";
                               
                                      } elseif( $x <= 30 ){ echo "x is less then 30";
                                                   
                                                      }else { echo "all conditions are false";}
                                
   ?>

the switch case statement: