PHP

PHP Conditional Statements

Conditional statements in PHP nothing but an action which works on different situation or condition .

In PHP following are the conditional statements

  • if statements
  • if-else statements
  • if else if else.. statements
  • Switch statements

If statements

This statement is use only when one condition is true

Example

<?php
  $i=1;
  if($i<=10) {
    echo "i = ".$i;
    $i++;
   }
?>

In this example their is an variable $i which have a value 1 and their is a condition in if statement that variable $i is less than equal to 10 .so statement inside if execute until the $i is greater than 10.

if-else statements

This statement is used when a condition is true then if block statement is executed and if the condition in if-statement is false then else block statement is executed

Example

<?php
  $i=10;
  if($i%2==0) {
      echo $i."is an even  Number";
  } else { 
    echo $i."is an odd Number";
  }
?>

in this example their is a variable $i which have a value 10. In if statement their is a condition that if $i divided by 2 and reminder becomes zero .so the $i is an even number otherwise else statement is executed.

if elseif …. else statements

These statements are used when more than two condition is available for any situation or condition

for example

<?php
  $i = 12;
  if($i<12 && $i>0) {
     echo "Hello Gentleman";
  } elseif($i>12 && $i<20) {
     echo "How are You?";
  } else {
     echo "Good morning!";
  }
?>

In above example we have a variable $i which have a value 12.their are three condition one in if statement and other in elseif statement and last else statement .So if statement condition tells that $i is greater than 0 and $i is less than 12.so if condition is true then if statement is executed otherwise it will go on next condition elseif.and then it also check condition if this elseif statement condition is false or true .if true executes the statement otherwise else condition is true

Switch statement

PHP provides another control structure called a “switch” statement. Switch statements are similar to a series of IF statements that use the same expression. Switch statements allow you to compare many different values with the same variable or expression. For example: you may want to look at the day of the week, and perform different actions based on which day of the week it is.

For example:

<?php
     switch (date('l')) {
        case "Monday":
          echo "Wash on Monday";
          break;
       case "Tuesday":
          echo "Iron on Tuesday";
          break;
       case "Wednesday":
          echo "Mend on Wednesday";
          break;
       case "Thursday":
          echo "Churn on Thursday";
          break;
       case "Friday":
          echo "Clean on Friday";
          break;
       case "Saturday":
          echo "Bake on Saturday";
          break;
       case "Sunday":
          echo "Rest on Sunday";
          break;
       default:
          echo "I don't know what day it is";
          break;
     }
?>

So in above example their is a function date(‘l’) this function finds the current day of the week inside this single quotes this is small L.So switch statements execute according to current day like if date(‘l’) gives you Saturday so it will see the case which have “Saturday” if the case is found so it will execute that case block.if case is not found then default block of statement code is executed.inside case you can also write if ,if else,if elseif else satements according to your requirements

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button