How to PHP Scripting- If Else & Elseif statement

In this guide, we are going learn PHP Conditional statements. The if statement is the used to execute a block of code only if the specified condition evaluates to true. It can not do anything if the condition is false.

In the PHP language, we have 4 types of conditional statements.

1.if statement:  this statement is used to execute some code if some specific condition is true.
2.if else statement: this statement to execute some code if the conditions are true. If the condition is not true do something different.
3.if elseif else statement – can be used to select one of the several blocks of code (using different conditions) to be executed.
4.switch statement – selects one of many blocks of code to be executed.

 

If statement.

 

Syntax
     if(condition){
         // Code to be executed
     }

 

If else statement.

 

Syntax
     if (condition) {
         code to be executed if condition is true;
     } else {
         code to be executed if condition is false;
     }

An example of the if  else statement:

Example
     <?php
       $d = date("D");
       if($d == "Fri"){
          echo "Have a nice weekend!";
       } else{
          echo "Have a nice day!";
       }
     ?>

 

If elseif else statement.

elseif is a combination of if and else. It extends an if statement to execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false.

 

Syntax
     <?php
      if (condition) {
         code to be executed if this condition is true;
      } elseif (condition) {
         code to be executed if this condition is true;
      } else {
         code to be executed if all conditions are false;
      }
     ?>

An example of the if  elseif else statement:

Example
     <?php
       if ($x > $y) {
         echo "x is bigger than y";
       } elseif ($x == $y) {
         echo "x is equal to y";
       } else {
         echo "x is smaller than y";
       }
     ?>

 

Switch statement.

The switch statement is used to select one of many blocks of code to be executed. The switch statement is used to alter the flow of a program.

 

Syntax
	<?php
	 switch (n) {
		case label1:
			//code to be executed if n=label1;
			break;
		case label2:
			//code to be executed if n=label2;
			break;
		case label3:
			//code to be executed if n=label3;
			break;
		default:
			//code to be executed if n is different from all labels;
	 }
	?>

An example of the switch statement:

Example
	<?php
	   $d = date("D");       
	   switch ($d){
		  case "Mon":
			 echo "Today is Monday";
			 break;         
		  case "Tue":
			 echo "Today is Tuesday";
			 break;            
		  case "Wed":
			 echo "Today is Wednesday";
			 break;            
		  case "Thu":
			 echo "Today is Thursday";
			 break;
		  case "Fri":
			 echo "Today is Friday";
			 break;           
		  case "Sat":
			 echo "Today is Saturday";
			 break;     
		  case "Sun":
			 echo "Today is Sunday";
			 break;        
		  default:
			 echo "Wonder which day is this ?";
		}
	?>

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...