Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

PHP Conditional Statements, if else and else if in php

Last modified on May 26th, 2018 at 2:03 pm

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
<?phpStarting php
$S = date(“D”);Declaring variable
if ($S < “15”)If condition true code executes
 echoBuilt 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

CodeComments
<?phpStarting php
$d= day(“D”);Declaring variable
if ($d == “Sunday”)If condition true code executes
echoBuilt in function to print the text
elseIf 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

CodeComments
<?phpStarting php
$result = 70;Declaring variable and value
if ($result >= 75)If condition true code executes
echoBuilt in function to print the text
elseif ($result >= 60)If condition true code executes
echoBuilt in function to print the text
elseif($result >= 45)If condition true code executes
echoBuilt in function to print the text
elseIf condition  false code executes
echoBuilt in function to print the text
?>Closing of php

 

Prof. Fazal Rehman Shamil
Researcher, Publisher of International Journal Of Software Technology & Science ISSN: 2616-5325
Instructor, SEO Expert, Web Programmer and poet.
Feel free to contact.