PHP Conditional Statements

Php Control Structure

Control structure uses to control the flow of the program. 

This structure divide into mainly two parts

  1. Conditional statements.
  2. Looping statements


PHP Conditional Statements

  • The program executed sequentially (line by line). 
  • Control structure controls the flow of program code’s execution in the application. 
  • Allows random execution in code (not sequential). 
  • Use certain conditions. 
  • Performs different actions which are based on different conditions.

PHP provides the following conditional statements:

  1. If statement (simple) - executes some code if one condition is true
  2. If...else statement - executes some code if a condition is true and another code execute when if the condition is false (else part execute)
  3. If...elseif...else statement - executes different codes for more than two conditions. Else part executes when all if and end if are false.
  4. The nested if...else statements- if...else statements inside an if...else statement the statements are nested
  5. Switch statement - selects one of many blocks of code to be executed (action code executes according to true condition block.)

PHP - The If Statement

The if statement executes some code if one condition is true.


Syntax

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

Example

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
}
?>

Output:-

 "Have a good day!" if the current time (HOUR) is less than 20:


Note:- date(“H”)   show system time.

            date(“D”)   show system date.

================================================

PHP - The if...else Statement

The if...else statement executes some code if a condition is true and another code if that condition is false.

Syntax

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

else {
  code to be executed if condition is false;
}

Example

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";

else {
  echo "Have a good night!";
}
?>

output: -

Have a good night!


Ex.

<?php

if($age < 18)

{ echo 'Child'; // Display Child if age is less than 18 }

else{ echo 'Adult'; // Display Adult if age is greater than or equal to 18 }

?>

Output:-

Child

================================================

PHP - The if...elseif...else Statement

The if...elseif...else statement executes different codes for more than two conditions.


Syntax

if (condition) {
  code to be executed if this condition is true;
}

elseif (condition) {
  code to be executed if the first condition is false and this condition is true;
}

else {
  code to be executed if all conditions are false;
}

Example

<?php
$t = date("H");

if ($t < "6") {
  echo "Have a good morning!";
} elseif ($t < ="12") {
  echo "hello good day!";
} else {
  echo " good evening!";
}
?>

Output:-

hello good day!

============================================================

The nested if...else statements

When you find if...else statements inside an if...else statement the statements are nested. With this statement, you can get alternatives results when a condition is true or false.



Syntax:
    if (condition 1 )
    {
                   if (condition 2 )
                   {
                                  //  code1 to be executed
                   }
                   else
                   {
                                   // code 2 to be executed
                   }
    }
    else
    {
                   // code 4 to be executed
    }

Example:

compare two numbers using the nested if statement.

<?php

// defining variables
$number1 = 40;
$number2 = 12;
if ($number1 != $number2) {
    echo 'number1 is different from number2';
    echo '<br>';
    if ($number1 > $number2) {
        echo 'number1 is greater than number2';
    } else {
        echo 'number2 is greater than number1';
    }
} else {
    echo 'number1 is equal to number2';
}
?>

Output

number1 is different from number2
number2 is greater than number1

example:- 

<?php

//variable definition

$gender = 'M';

switch ($gender) {

    case 'F':

        echo 'F is FEMALE';

    break;

   case 'M':

        echo 'M is MALE';

    break;

    default:

        echo 'Invalid choice';

}

?>

Output

M is MALE

============================================================================= 

The PHP switch Statement

Used to perform different actions based on different conditions.

Use the switch statement to select one of many blocks of code to be executed.

Syntax

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;
}


in the switch case :-

  • first, a single expression n (mostely a variable), that is evaluated once. 
  • The value of the expression is compared with the case label conditions in the switch case. 
  • The block of code executed with in that case. 
  • Use break statements to prevent the code from running into the next case automatically, it sends to control to out of switch case. 
  • The default statement is used if no match is found.

Example

<?php
$favcolor = "red";

switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
    break;
  case "blue":
    echo "Your favorite color is blue!";
    break;
  case "green":
    echo "Your favorite color is green!";
    break;
  default:
    echo "Your favorite color is neither red, blue, nor green!";
}
?>

Output:-

Your favorite color is red!


Example:-

<html>

   <body>

      <?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 ?";

         }


     ?>

   </body>

</html>


Post a Comment

0 Comments