PHP Conditional Statements

In this PHP tutorial, you will learn all about conditional statements in PHP. We will discuss in detail the different types of conditional statements namely if, if … else, if… else if … else, nested if, switch, ternary operator.

What is if Statement in PHP?

The if statement will execute its code block if the given condition becomes true.

Syntax


if (condition) {
  code to be executed if condition is true;
}
 
PHP - If

Example :if Statement in PHP


$a = 20;
$b = 18;
if($a > $b){
    echo "$a is the greatest…";
}

 

Output:


20 is the greatest…

In the above example, we are comparing the two variables and printing the greatest of them. In this program, we are using the if statement where we check the condition “a > b” if it becomes true, we will print that the value of the variable a is the greatest and exit the program and if it is false then we will exit the program.

What is if … else Statement in PHP?

In if … else statement we first check the condition if the condition is true, we perform the if code block or if the condition is false then we will perform the else code block.

Syntax


if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}
 
PHP - If

Example :if Statement in PHP


$a = 10;
$b = 17;
if($a > $b){
    echo "$a is the greatest…";
} else {
    echo "$b is the greatest…";
}

 

Output:


17 is the greatest…

if … else if … else Statement in PHP

If we have to perform multiple condition checks and for every condition, we have to perform different code blocks we use the if … else if … else statement. It first checks the condition in if and if it is true, it performs the if code block and if it is false, it checks the condition in else if and if it is true if performs its code block and we can perform multiple else if blocks and if every condition becomes false by default it will perform the else block.

Syntax


if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

 
PHP - If

Example :if … else if … else Statement in PHP


$a = 10;
$b = 17;
$c = 8;
if($a > $b && $a > $c){
    echo "$a is greatest...";
}
else if($b > $c){
    echo "$b is greatest...";
}
else{
    echo "$c is greatest...";
}
 

Output:


17 is the greatest…

In the above example, we are comparing the three variables and printing the greatest of them. In this program we are using the if … else if … else statement where we check the condition “a > b” and “a > c” if both are true, we will print that the value of the variable “a” is the greatest and exit the program and if it is false then we will check the condition “b > c” if true then we print the value of the variable “b” as the greatest and if both the conditions are false we print the value of the variable “c” as the greatest.

What is nested if Statement in PHP?

In the nested, if statement the if code block will contain another if statement. This inner if statement will only be executed if the outer condition becomes true.

Syntax


if (condition) {    
code to be executed if condition is true   
if (condition) {    
code to be executed if condition is true    
}    
}  
 
PHP - Nested If

Example :Nested if Statement in PHP


$a = 19;
$b = 17;
$c = 15;
if($a > $b){
    if($a > $c){
        echo "$a is greatest...";
    }
}
else if($b > $c){
    echo "$b is greatest...";
}
else{
    echo "$c is greatest...";
}

 

Output:


19 is greatest...

In the above example we are comparing the three variables and printing the greatest of them. In this program we are using the nested if statement where we check the condition “a > b” if it is true, we will then check the condition “a > c” if it is true, we print that the value of the variable “a” is the greatest and exit the program and if it is false then we will exit the block of the outer if code block and then check the condition “b > c” if true then we print the value of the variable “b” as the greatest and if both the conditions are false we print the value of the variable “c” as the greatest.

What is switch Statement in PHP?

In the switch statement we pass an expression and the different cases are checked and for each match different statements are performed. And at the end the default block will be performed.

Syntax


switch (n) {
  case label1:
    code to be executed if n=label1;
    break;
  case label2:
    code to be executed if n=label2;
    break;
  case label3:
    code to be executed if n=label3;
    break;
    ...
  default:
    code to be executed if n is different from all labels;
}

 
PHP - If

Example :if Statement in PHP


$ch = 6;  
    switch ($ch)  
    {     
        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";  
            break;  
    }  
 

Output:


Saturday

In this program we are going to print the day of the week. For that we are first assigning the day in number to the variable “ch” then in the switch statement we pass the value of the variable “ch” then in the block of the switch statement we write the case for every alternative. and at last we use a default statement for the performing the operation where every case becomes fail.