PHP IF...ELSE Statement

  • if ... else statement -  if...else statement is used when a different sequence of instructions is to be executed depending on the logical value ( True / False ) of the condition evaluated.
The Syntax is as follows :
     
      if(condition)
           statement - 1 ;
     else
          statement - 2 ;
     statement - 3;
OR
     if(condition)
       {
            Statements - 1
       }
    else
     {
           Statement - 2
     }
 Statement - 3

if the condition is true, then the sequence of statement (statement - 1) execute; other wise the statement - 2 following the else part of if-else statement will get executed. In both the cases,the control is then transferred to statement - 3 to follow sequential execution of program.

Example : 
    To print whether the given number is even or odd.

<?php

       $x = 10;
       if($x % 2 == 0)
        {
           echo "<br/> The Given number is even";
        }
    else
       {
          echo "<br/>The Given number is odd";
      } 
?>


Comments