PHP Conditional Statement

Conditional Statement is an important topic in PHP programming language. This statement is used to take various decisions in PHP programming. Say, if the username and the password of an user are matched, the user will get access to the application otherwise he/she will be denied. Conditional Statement is triggered when the expression is true. So, Conditional Statement is mostly depended on Comparison or Relational Operator. If you are new in PHP programming, feel free to study PHP Comparison or Relational Operator because it will help you to understand PHP Conditional Operator so easily.

PHP Conditional Statement

PHP Conditional Statement

PHP Conditional Statement is divided into five categories. These are…

  • if Conditional Statement
  • if-else Conditional Statement
  • Nested if-else Statement
  • else if ladder Conditional Statement
  • switch Statement

if Conditional Statement

if Conditional Statement is trigger when its expression is true. The structure of if Conditional Statement is given below…

if(expression){
// if statement’s body goes here
}
OR
if(expression) :
// if statement’s body goes here
endif;

The structure of if Conditional Statement says, if expression is true, the body will be executed otherwise the body will be ignored. 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 Boolean true or false value.  From Operator and Expression in PHP we know that Comparison or Relational Operators and Logical Operators return Boolean true or false value. Again, from Data Types and Variable in PHP 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, you have to use Comparison Operators, Logical Operators or Variable as an expression of PHP Conditional Statement.

See below PHP code where if Conditional Statement is used…


<?php

$a=2;

$b=4;

if($a<$b){

echo $a. “ is smaller than ”.$b;

}

?>;

Output: 2 is smaller than 4.

Since the expression is true, the body statement will be executed. See below example where a variable is used as an expression.


<?php

$a=5;

if($a) {

echo “Variable a contains ”. $a;

}

?>

Output: Variable a contains 5.

As variable $a contains any value without NULL and 0, the body will be executed. If any variable contains NULL or 0, it returns Boolean false value.

if-else Conditional Statement

if-else Conditional Statement have to be used in such a situation where one event must be done. For example, if you do well, I shall give you award otherwise I shall give you punishment. Here one event must happen. So, if-else Conditional Statement should be used in this condition. The structure of  a if-else Conditional Statement is shown below…

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

The above structure says that if the expression 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.

See below example where two numeric values are compared with if-else conditional statement and one block must execute to show proper result.


<?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.

Ternary Operator

Ternary Operator can be used to express if-else Conditional Statement shortly. The structure of Ternary Operator is given below…

Expression ? return if expression is true : return if expression is false;

For example, the above example can be expressed with Ternary Operator as below…


<?php

$a=10;

$b=15;

$result=($a<$b)? “$a is smaller” : “$b is smaller”;

echo $result;

?>

Output: 10 is smaller.

Nested if-else Statement

When one if-else statement is used within another if-else statement, then it is called nested if-else statement. Nested if-else statement is often used in PHP programming. Finding greater number within three numeric values is a simple example of a nested if-else statement. PHP program to find greater number within three numeric values using nested if-else statement is given below…


<?php

$a=5;

$b=6;

$c=10;

if ($a>$b){

if($a>$c){

echo $a.“ is greater.”;

}else{

echo $c. “ is greater”;

}

}else{

if($b>$c){

echo $b.” is greater”;

}else{

echo $c.“ is greater ”;

}

}

?>

Output: 10 is greater.

else if Ladder Conditional Statement

else if ladder Conditional Statement is used to select one expression within multiple expressions . The structure of a else if ladder Conditional Statement is given below…

if(expression 1){
}elseif(expression 2){
}elseif(expression 3){
}else{
}

Three expressions are declared in the above example. If any expression of these three is true, that block will be executed. If none of these is true, else block will be executed.

Say, you need to find obtained grade of a student from his obtained mark. Such a situation, you can use else if ladder Conditional Statement. PHP program to find a grade mark with else if ladder Conditional Statement is given below…


<?php

$mark=75;

if($mark>=80){

echo “You have got A+”;

}elseif($mark<80 && $mark >=70){

echo “You have got A ”;

}elseif($mark<70 && $mark >=60){

echo “You have got A-”;

}elseif($mark<60 && $mark>=50){

echo “You have got B”;

}elseif($mark<50 && $mark>=40){

echo “You have got C”;

}else{

echo “Please try again later.”;

}

?>

Output: You have got A.

From the above code you can see that only one expression is being executed within a series of expressions. This is a simple application of else if ladder Conditional Statement.

switch Statement

The switch Statement is also known as switch-case Statement. switch-case Statement is another way to select one expression within multiple expressions. you can use switch statement as an alternative of else if ladder Conditional Statement. The structure of switch Statement is given below…

switch(expression){
case result1:
break;
case result2:
break;
case result3:
break;
default:
}

Here the expression can be any valid PHP expression and it is not necessary that the expression must return true or false like if Conditional Statement. In switch statement, the result of 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.

You can do the previous obtained grade application with switch statement and get the same output. PHP program to obtain grade mark with switch statement is given below…


<?php

$a=75;

switch($a){

case $a>=80:

echo “You have hot A+”;

break;

case $a<80 && $a>=70:

echo “You have got A”;

break;

case $a<70 && $a>=60:

echo “You have got A-”;

break;

case $a<60 && $a>=50:

echo “You have got B”;

break;

case $a<50 && $a>=40:

echo “You have got C”;

break;

default:

echo “Please try again later.”

}

?>

Output: You have got A.

PHP Conditional Statement has been explained in this article from very beginning. If you have any problem to understand PHP Conditional Statement, feel free to contact with me from Contact us page. I’ll try my best to stay with you. PHP Loop statement, an important topic in PHP programming, will be explained from very beginning in next article.  Spend some times to study the article and improve your idea about PHP Loop Statement.

2 Comments
  1. cheap snapbacks
  2. discount oakley sunglasses

Leave a Reply to discount oakley sunglasses Cancel reply

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