PHP Conditional Statements, if else and else if in php

PHP Conditional Statements:

In PHP following are Conditional statements:

  • if statement if any one condition is true then some code executes…
  • ..elseif….else statement  it executes different codes for more than two conditions.
  • switch statementit selects many blocks of code to be execution…

 The syntax of if Statement

                      if (condition)

           {

                     When condition true code executes;

          }

 Example 

 

Output

Working day

Explanation of code

Code  Comments
<?php Starting php
$S = date(“D”); Declaring variable
if ($S < “15”) If condition true code executes
 echo Built in function to print the text
?> Closing of php

   

   if…else Statement

If the condition is True then it executes the True part of the statements, otherwise, it will execute else part of the statements.

   Syntax of if…else Statement

    if (condition)

    {
    When condition true code executes;
    }

      else

    {
   When condition false code executes;
    }

   Example of if…else Statement

 

Output

Have a nice day

Code Comments
<?php Starting php
$d= day(“D”); Declaring variable
if ($d == “Sunday”) If condition true code executes
echo Built in function to print the text
else If condition false code executes
echo

?>

Built in function to print the text.

Closing of PHP

 

 if…else if….else Statement

Here we will check multiple conditions to get them True, If none of the conditions is True, then else part will execute.

      Syntax of if…else if….else Statement

      if (condition)

       {
     When condition true code executes;
        }

      else if (condition)

       {
      When condition true code executes;
        }

      else

        {

      When all conditions false code executes;
        }

 Example of if…else if….else Statement

 

     Output:

      Pass.. Grade B

Code Comments
<?php Starting php
$result = 70; Declaring variable and value
if ($result >= 75) If condition true code executes
echo Built in function to print the text
elseif ($result >= 60) If condition true code executes
echo Built in function to print the text
elseif($result >= 45) If condition true code executes
echo Built in function to print the text
else If condition  false code executes
echo Built in function to print the text
?> Closing of php