‘If… Else’ statement is used to perform different action for different decision. Conditional (relational operator) statements are used to perform different action on basis of different decision. So we can say that ‘If…Else’ statement generally used with conditional statement.

In PHP, we have several conditional statements such as:

‘If’ statement: If statement is used to execute line of code at some specified condition.

Let’s we have an example, how to implement ‘If’ statement in php.
Syntax:

If(condition)

   code to be executed if condition is true;

Example:
<html>
     <head>
       <title> If statement</title>
     </head>
<body>
       <?php
             
              // Declares variables
              $value = 50;
 
              // if statement block is started at first specified condition
              if($value == 50)
               {
                   // code for execution
                   echo "This is if statement block at (value == 50) condition "; 
               }
             
              // if statement block is started at second specified condition
              if($value == 25)
               {
                   // execution code
                 echo "This is if statement block at (value == 50) condition ";   
               }
             
         ?>
</body>
 
</html>
Output:

If…Else Statement in PHP

From above example it is clear that if statement block is executed when specified condition will be right.

If…Else Statement: Use If…Else statement when specified condition is true or false. That is If… Else statement executes line of code even; it is in true condition or false condition.

To more understand about If…Else statement, let’s we have an example, how to implement If…else condition.

Syntax:
If(condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;

 

Example:
<html>
     <head>
       <title> If statement</title>
     </head>
<body>
       <?php
             
              //  retrieve currentmonthname from date() function
              $month =  date("M");
      
              // if...else statement block is started
              if($month == "Aug" )
               {
                   // if above condition true
                   echo "This is if statementblock ";
               }
             
              else
               {
                  // if above condition not true
                 echo " This iselse statement block";
                }
         ?>
</body>
 
</html>

 

Output:

If…Else Statement in PHP

Nested if…else Statement:  Use nested if…else statement to select one of several code block to be executed.

Let’s we have an example, how to implement nested if…else statement in php.

Syntax:
If(condition)
     code to be executed if condition is true;
else if(condition)
     code to be executed if condition is true;
else if(condition)
     code to be executed if condition is true;
.......
.......
.......
else
     code to be executed if condition is false;

 

Example:
<html>
     <head>
       <title> If else if statement</title>
     </head>
<body>
       <?php
             
              // Declare variables name
              $total  = 500 ;
              $gain  = 360 ;
              $percent  = ($gain/$total )* 100;
 
              if($percent >=75)
               {
                     print "Distinction";
                }
              else if ($percent >59 && $percent <=74)
               {
                     print "First division";
                }
              else if ( $percent >=45 && $percent <=59)
                {
                     print "second Division";
                }
              else if ($percent >=33 && $percent <45 )
              {
                     echo "Third Division";
              }
              else
              {
                echo "Fail";
              }
 
         ?>
</body>
 
</html>
Output:

If…Else Statement in PHP


Leave Comment