• PHP Video Tutorials

PHP - If…Else Statement



The ability to implement conditional logic is the fundamental requirement of any programming language (PHP included). PHP has three keywords (also called as language constructs) – if, elseif and else – are used to take decision based on the different conditions.

The if keyword is the basic construct for the conditional execution of code fragments. More often than not, the if keyword is used in conjunction with else keyword, although it is not always mandatory.

If you want to execute some code if a condition is true and another code if the sme condition is false, then use the "if....else" statement.

Syntax

The usage and syntax of the if statement in PHP is similar to that of the C language. Here is the syntax of if statement in PHP −

if (expression)
   code to be executed if expression is true;
else
   code to be executed if expression is false;

The if statement is always followed by a Boolean expression.

  • PHP will execute the statement following the Boolean expression if it evaluates to true.

  • If the Boolean expression evaluates to false, the statement is ignored.

  • If the algorithm needs to execute another statement when the expression is false, it is written after the else keyword.

Example

Here is a simple PHP code that demonstrates the usage of if else statements. There are two variables $a and $b. The code identifies which one of them is bigger.

<?php
   $a=10;
   $b=20;
   if ($a > $b)
      echo "a is bigger than b";
   else
      echo "a is not bigger than b";
?>

When the above code is run, it displays the following output

a is not bigger than b

Interchange the values of "a" and "b" and run again. Now, you will get the following output −

a is bigger than b

Example

The following example will output "Have a nice weekend!" if the current day is Friday, else it will output "Have a nice day!" −

<?php
   $d = date("D");

   if ($d == "Fri")
      echo "Have a nice weekend!"; 
   else
      echo "Have a nice day!"; 
?>

It will produce the following output

Have a nice weekend!

Using endif in PHP

PHP code is usually intermixed with HTML script. We can insert HTML code in the if part as well as the else part in PHP code. PHP offers an alternative syntax for if and else statements. Change the opening brace to a colon (:) and the closing brace to endif; so that a HTML block can be added to the if and else part.

<?php
   $d = date("D");

   if ($d == "Fri"): ?>
      <h2>Have a nice weekend!</h2>

   <?php else: ?>
      <h2>Have a nice day!</h2>
<?php endif ?>

Make sure that the above script is in the document root of PHP server. Visit the URL http://localhost/hello.php. Following output should be displayed in the browser, if the current day is not a Friday −

Have a nice day!

Using elseif in PHP

If you want to execute some code if one of the several conditions are true, then use the elseif statement. The elseif language construct in PHP is a combination of if and else.

  • Similar to else, it specifies an alternative statement to be executed in case the original if expression evaluates to false.

  • However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true.

if (expr1)
   code to be executed if expr1 is true;
elseif (expr2)
   code to be executed if expr2 is true;
else
   code to be executed if expr2 is false;

Example

Let us modify the above code to display a different message on Sunday, Friday and other days.

<?php
   $d = date("D");
   if ($d == "Fri")
      echo "<h3>Have a nice weekend!</h3>";

   elseif ($d == "Sun")
      echo "<h3>Have a nice Sunday!</h3>"; 

   else
      echo "<h3>Have a nice day!</h3>"; 
?>

On a Sunday, the browser shall display the following output

Have a nice Sunday!

Example

Here is another example to show the use of if–elselif–else statements −

<?php
   $x=13;
   if ($x%2==0) {
      if ($x%3==0) 
         echo "<h3>$x is divisible by 2 and 3</h3>";
      else
         echo "<h3>$x is divisible by 2 but not divisible by 3</h3>";
   }

   elseif ($x%3==0)
      echo "<h3>$x is divisible by 3 but not divisible by 2</h3>"; 

   else
      echo "<h3>$x is not divisible by 3 and not divisible by 2</h3>"; 
?>

The above code also uses nestedif statements.

For the values of x as 13, 12 and 10, the output will be as follows −

13 is not divisible by 3 and not divisible by 2
12 is divisible by 2 and 3
10 is divisible by 2 but not divisible by 3
Advertisements