PHP if, if...else, if...elseif...else Statements

This article is created to cover the three conditional statements used in PHP, that are:

  1. if
  2. if...else
  3. if...elseif..else

PHP if Statement

The PHP if statement is used, when we need to execute some block of statements (codes) when a specified condition is true. For example:

<?php
   $num = 10;
   if($num==10)
   {
      echo "The value of \$num is 10";
      echo "<BR>";
   }
?>

The output produced by above PHP example is shown in the snapshot given below:

php if statement

In above example, since the condition $num==10 or 10==10 (as the value of $num has initialized by 10) evaluates to be true. Therefore the program flow goes inside the if body, and executes the two written statements. The first statement print the value of $num, whereas the second statement inserts a line break, using the BR tag.

The above program, can also be written in this way:

<?php
   $num = 10;
   if($num==10)
      echo "The value of \$num is 10<BR>";
?>

Since, there is a single statement available as the body of if, then there is no need to put the statement inside a {} (curly braces). Single statement does not require to write inside a curly braces. However, you can use curly braces for single statement too.

The if Statement Syntax in PHP

The syntax of if statement in PHP, is:

if(condition)
{
   // block of code to execute, if condition is true
}

PHP if...else Statement

The PHP if...else block is used when we need to execute some block of code, when the specified condition evaluates to be true, and some other block of code, when the specified condition evaluates to be false. For example:

<?php
   $num = 20;
   if($num==10)
      echo "The value of \$num is 10";
   else
      echo "The value of \$num is not 10";
?>

The output of this PHP example should be:

The value of $num is not 10

Since the condition $num==10 or 20==10 evaluates to be false, therefore the program flow goes inside the else body, and the statement available inside the else body gets executed.

That is, the program executes the block of code available in the body of if, when the condition evaluates to be true. Otherwise the program executes the block of code available in the body of the counterpart of if, that is else, when the condition evaluates to be false.

The if...else Statement in PHP

The syntax of if...else statement in PHP, is:

if(condition)
{
   // block of code to execute, if condition is true
}
else
{
   // block of code to execute, if condition is false
}

PHP if...elseif...else Statement

The PHP if...elseif...else statement is used when we need to execute particular block of code based on multiple conditions. For example:

<?php
   $num = 0;
   if($num<0)
      echo "The value of \$num is a negative number.";
   elseif($num==0)
      echo "The value of \$num is 0.";
   else
      echo "The value of \$num is a positive number.";
?>

The output of this PHP example should be:

The value of $num is 0.

The if...elseif...else Statement in PHP

The syntax of if...elseif...else statement in PHP, is:

if(condition_1)
{
   // block of code to execute, if condition_1 is true
}
elseif(condition_2)
{
   // block of code to execute, if condition_2 is true
}
elseif(condition_3)
{
   // block of code to execute, if condition_3 is true
}
elseif(condition_4)
{
   // block of code to execute, if condition_4 is true
}
.
.
.
elseif(condition_N)
{
   // block of code to execute, if condition_N is true
}
else
{
   // block of code to execute, if all conditions are false
}

In case if multiple conditions evaluates to be true, for example, let's suppose if condition_2 and condition_3, both evaluates to be true. Then in that case, the first condition, which evaluates to be true, gets executed, and rest gets skipped. For example:

<?php
   $x = -10;
   if($x > -10)
      echo "The value of \$x is greater than -10";
   elseif($x == -10)
      echo "The value of \$x is -10";
   elseif($x < 0)
      echo "The value of \$x is less than 0";
   elseif($x == 0)
      echo "The value of \$x is 0";
   elseif($x > 0)
      echo "The value of \$x is greater than 0";
   else
      echo "The value of \$x is unknown";
?>

Since the condition $x == -10 and $x < 0, both are true, but first the condition $x == -10 has been evaluated, therefore the block of code available inside that block will be executed, and rest of all the execution of same if...elseif...else structure gets skipped. Therefore, the output should be:

The value of $x is -10

PHP Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube