PHP Conditional Statements Explanation

Conditional Statements are frequently used subject in PHP programming language. No single PHP program can be possible without conditional statements. So, a newbie programmer should have better understanding on PHP conditional statements. Conditional statements are mainly used to take various logical decisions in PHP programming and PHP relational operators are frequently used in PHP conditional statements. In my previous article I explained PHP operators and expression and in this article I will discuss PHP Conditional Statements elaborately. 

Conditional Statement in PHP

Conditional Statements in PHP

Actually no program is possible without conditional statements. So, like other programming language conditional statements are also used in PHP programming language. PHP Conditional Statements can be divided into the following five categories.

  • PHP if clause or if condition
  • PHP if else condition
  • PHP nested if else condition
  • PHP else if ladder or multiple else if condition
  • PHP switch case condition

PHP if Clause

PHP if condition is used to check a single condition. PHP if clause is trigger when its expression becomes true. PHP if conditional statement is expressed like the following structure. 

if(expression){
// The body of if clause goes here
}
OR
if(expression) :
// the body of if statement goes here
endif;

The above structure says, if the expression becomes true, execute the body otherwise ignore the if clause and go ahead. The body is considered from starting curly brace ({) to ending curly brace (}) or from colon (:) to endif statement.

The expression of conditional statement should be such a statement which returns a Boolean true or false value. We know that comparison or relational operators and logical operators return Boolean true or false value. We also know that a PHP variable returns two types of value. Firstly, it returns its assigned data type value and secondly it returns either true or false value. If any value is assigned into a variable, it returns Boolean true otherwise it returns Boolean false. So, we have to provide Comparison Operators, Logical Operators or Variable as an expression of PHP Conditional Statement.

The following program is an example of PHP if clause or if condition usage.

<?php
$a=2;
$b=4;
if($a<$b){
   echo $a. “ is smaller than ”.$b;
}
?>

Output

2 is smaller than 4

In the above program, the expression of PHP if condition is true. So, the body has been executed. The following program is another example of PHP if clause where a variable is being used as an expression.

<?php
$a=5;
if($a) {
  echo “Variable a contains ”. $a;
}
?>

Output

Variable a contains 5

As $a contains a numeric value without NULL and 0, the body has been executed. If any variable contains NULL or 0, it will return Boolean false and if clause will be ignored in this case.

PHP if else Condition

PHP if else conditional statement has to be used in such a situation where one event between two events must be done. For example, if it rains today, I will go to play football otherwise I will go to school. Here one event must happen. So, the if else condition should be used in this situation. The PHP if else condition is expressed like the following structure.

if(expression){
// the body of if statement goes here
} else {
// the body of else statement goes here.
}

The above structure says that if the expression of if clause is true, the body of if block will be executed otherwise the body of else block will be executed. That means one body must always be executed.

The following example is an example of PHP if else condition.

<?php
$a=10;
$b=15;
if($a<$b){
  echo “The greater number is ”. $b;
}else{
  echo “The greater number is ”. $a;
}
?>

Output

The greater value is 15

PHP Ternary Operator

PHP Ternary Operator is a shorthand or short syntax of PHP if else condition that represent if else condition in one line. PHP ternary operator is expressed like the following structure.  

Expression ? true conditional statement: false conditional statement;

If the expression is true, the true conditional statement will be executed otherwise the false conditional statement will be executed.

For example, the above if else program can be expressed with the ternary operator like the following program.

<?php
$a=10;
$b=15;
$result=($a<$b)? “The greater number is ”.$b : “The greater number is ”.$a;
echo $result;
?>

Output

The greater value is 15

PHP nested if else Condition

If one PHP if else statement is used within another if else statement, it is called nested if else. Nested if else is frequently used in PHP programming. For example, getting greater number within three numeric values can be used the PHP nested if else condition.

The following program is an example of usage of nested if else condition to find greater value within three numeric values.

<?php
$a=5;
$b=6;
$c=10;
if ($a>$b){
 if($a>$c){
  echo $a.“ is greater value”;
 }else{
  echo $c. “ is greater value”;
 }
}else{
  if($b>$c){
    echo $b.” is greater value”;
  }else{
    echo $c.“ is greater value”;
  }
}
?>

Output

10 is greater value

PHP else if Ladder or Multiple else if Condition

PHP else if ladder condition is used to select one expression within multiple expressions. That means within multiple elseif expressions only one expression will be true and other expressions will be false and the body of the true expression will be executed. The else if ladder condition is expressed like the following structure.

if(expression 1){
// the body of expression 1
}elseif(expression 2){
// the body of expression 2
}elseif(expression 3){
// the body of expression 3
}else{
// the body of else block
}

If any expression of these three expressions becomes true, that block will be executed. If none of these three expression becomes true, the else block will be executed.

For example, we need to find obtained grade of a student from his obtained mark.  In this situation, we can use PHP else if ladder condition to determine his obtained grade based on obtained mark from multiple grading systems.

The flowing PHP program is an example of PHP else if ladder statement where one grade will be selected based on obtained mark within multiple grading systems.

<?php
$mark=75;
if($mark>=80){
  echo “You have got A+”;
}elseif($mark<80 &amp;&amp; $mark >=70){
  echo “You have got A ”;
}elseif($mark<70 &amp;&amp; $mark >=60){
  echo “You have got A-”;
}elseif($mark<60 &amp;&amp; $mark>=50){
  echo “You have got B”;
}elseif($mark<50 &amp;&amp; $mark>=40){
  echo “You have got C”;
}else{
  echo “Please try again later.”;
}
?>

Output

You have got A.

From the above program we can see that only one expression is being executed within a series of expressions. This is a simple application of PHP else if ladder conditional statement.

PHP switch case Condition

PHP switch case condition is another way to select one expression within multiple expressions. We can use switch statement as an alternative of PHP else if ladder condition. The PHP switch case condition is expressed like the following structure.

switch (expression){
case result1:
// the body of case result 1
break;
case result2:
// the body of case result 2
break;
case result3:
// the body of case result 3
break;
default:
// the body of default case
}

Here, the expression can be any valid PHP expression and it is not necessary that the expression must return true or false value like PHP if condition. In switch case statement, the result of switch expression is compared with the case expression. If both results are matched, the case block will be executed and other case block will be ignored. If no case is matched, the default block will be executed. The case block is considered from colon (:) to break statement.

As an example of PHP switch case condition we can do the previous obtained grade application. If we observe the two programs output, we will find that both programs will produce the same output. The following PHP program is an example of PHP switch case condition to find grade based on obtaining mark.

<?php
$a=75;
switch($a){
 case $a>=80:
  echo “You have hot A+”;
 break;
 case $a<80 &amp;&amp; $a>=70:
  echo “You have got A”;
 break;
 case $a<70 &amp;&amp; $a>=60:
  echo “You have got A-”;
 break;
 case $a<60 &amp;&amp; $a>=50:
  echo “You have got B”;
 break;
 case $a<50 &amp;&amp; $a>=40:
  echo “You have got C”;
 break;
 default:
  echo “Please try again later.”
}
?>

Output

You have got A.

PHP Conditional Statements has been explained in this article. I hope you will now be able to apply PHP conditional statements in your program properly. However, if you face any confusion to understand PHP conditional statement properly, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.

Related post:

php-conditional-statement-explanation

ABU SAYEED

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, JavaScript, AJAX, jQuery, WordPress and CodeIgniter framework. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a iTechSheet author.

3 thoughts on “PHP Conditional Statements Explanation

  1. Thank you for sharing superb informations. Your web-site is so cool. I’m impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found simply the info I already searched everywhere and simply couldn’t come across. What an ideal web-site.

Leave a Reply to discount oakley sunglasses Cancel reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.