Conditional Statemets (if...else) in PHP


Introduction

A conditional statement is also known as a decision statement. A decision statement is used for executing commands based on some situation or conditions. There are four types of conditional statements in PHP, which are as follows.

  • if statement
  • if...else statement
  • if...elseif....else statement
  • switch statement

The if Statements

The if statement has much importance in programming languages. An if statement executes commands when a condition is true. The syntax of an if statement is as follows:

Syntax of if statement

     if (condition)
     {
         message ; //condition is true
     }

An example of an if statement is as follows:

<?php
$age=24;
if($age<=25)
{
  echo "You are a Young man" ;
}

Output

In the above code we assign the value of age is 24 and check for the condition that age is less than or equal to 25. If the assigned value of age is less than 25 i.e. condition is true then the message you are a young man is displayed.

image1.jpg

The if...else statement

The if...else statement is also a decision statement. The if...else statement has two parts, one is if statement and second is else statement.
In the if statement we supply a condition and it blocks commands that execute when the condition is true. The else statement executes commands when the condition is false. The syntax of the if statement is as follows:

Syntax of if...else statement

     if (condition)
      {
         message; // condition is true
      }
    else
      {
         message; // condition is false
     }

An example of an if...else statement is as follows:

<?php
$percent=30;
if($percent>=33)
{
  echo "You are a ed" ;
}
else
{
  echo "You are Failed" ;
}

Output

In the above code we assign the percent = 30 and supply the condition (percent is greater than or equal to 33). The assigned percent is not greater than or equal to 33 i.e. condition is false, so display the message of else part, you have failed.

 image2.jpg

The if...elseif....else statement

The if...elseif....else statements are also the same as the if...else statement. The if...elseif....else statements have more than one condition and else statement. The syntax of if...elseif....else statements is as follows:

Syntax of if...elseif....else statement

    if (condition)
      {
         message; // condition is true
      }
    elseif (condition)
      {
         message; // condition is true
     }
    else
     {
         message; // condition is false
     }

An example of an if...else statement is as follows:

<?php
$percent=55;
if($percent>=60)
{
  echo "You have First division";
}
elseif ($percent>=45 && $percent<60)
{
  echo "You have Second division" ;
}
elseif ($percent>=33 && $percent <45)
{
  echo "You have Third division";
}
else
{
  echo "You are Failed";
}
?>

Output

In the above code we assigned the percent = 55 and supplied the multiple conditions. The assigned percent is greater than 45 and less than 60 i.e. satisfy the condition of second elseif statement. So display the message you have second division.

image3.jpg

The switch statement

The switch statement is also a decision statement. The switch statement has multiple choices. In the switch statement we use the case, default and break keywords. The case keyword is used to specify each choice. The default keyword is used to execute a statement when no case matchs any switch expression. The break keyword is used to terminate the switch case. The syntax of the switch statement is as follows:

Syntax of switch statement

   switch (n)
    {
     case 1:
            code to be executed if n=1;
            break;
     case 2:
            code to be executed if n=2;
            break;
    default:
            code to be executed if n is different from both case1 and case2;
     }

An example of a switch statement is as follows:

<?php
$day=5;
switch ($day)
{
case 1:
 
echo "Monday";
 
break;
case 2:
 
echo "Tuesday";
 
break;
case 3:
 
echo "Wednesday";
 
break;
case 4:
 
echo "Thursday";
 
break;
case 5:
 
echo "Friday";
 
break;
case 6:
 
echo "Saturday";
 
break;
case 7:
 
echo "Sunday";
 
break;
default:
 
echo "Wrong Choice";
}
?>

Output

In the above code we assign the value for day 2 and supply the many cases. The assigned value of day is 5 which matches with case 5, so display the message of case 5 i.e. Friday.

image4.jpg

Conclusion

So in this article you saw different types of conditional statements or decision statements. Using this article one can easily understand the conditional statements in PHP.

Some Helpful Resources

Up Next
    Ebook Download
    View all
    Learn
    View all