If Else in PHP: A Comprehensive Guide
The if else statement in PHP is a two-way conditional statement or a double selection statement. It evaluates a condition and executes one of two blocks of code based on the result of the condition.
- If the condition evaluates to true, the if block of code or statements is executed.
- Otherwise, if the condition evaluates to false, the else block of code is executed.
In essence, the if-else statement decides the execution path based on whether the test condition is true or false.
A one-way if statement executes a block of code if and only if the specified condition is true. If the condition evaluates to false, nothing will be done. Sometimes, we want to take alternative action when the specified condition is false. In this case, we will use a two-way decision making if-else statement.
The double selection if-else statement in PHP allows you to perform one operation if the condition is true and an alternative operation if the condition is false. This is where the “else” block comes into play.
The else block is always used in conjunction with the if statement, providing an alternative path of execution when the if condition evaluates to false.
Basic Syntax of If Else Statement in PHP
The basic syntax of using if else statement in PHP is as follows:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
In the syntax of an if else statement, the test condition consists of a Boolean expression that evaluates to either true or false. Each block represents either a single statement or a block of statements enclosed within curly braces ({ }).
The else block or clause is an optional part of the if-else construct. This means that the else block can be omitted if it is not required. This convention applies to all control statements in PHP.
Flowchart Diagram of If-else statement in PHP
The flowchart for two-way if-else statement in PHP programming is as shown below in the figure.
Let’s see with the help of the above flowchart diagram how the two-way if-else statement work in PHP.
If the condition evaluates to true, the conditional code block following if statement is performed. If the condition is false, the entire else block of code following the else is performed. In no case will both statements execute at a time.
Consider the following example code below:
Example 1:
<?php
$passMarks = 40;
if($passMarks >= 40) {
echo "You passed the exam.";
}
else {
echo "You failed the exam.";
}
?>
Output:
You passed the exam.
In this example, the boolean expression evaluates to true because the passMarks is greater than 40. Therefore, it will display a message “You passed the exam.” on the browser. In case it evaluates to false, the message “You failed the exam.” will display. As usual, you can omit braces if there is only one statement within the block.
Substitution of If else statement with Conditional operator
You can also replace if else statement with conditional operator (?:). Consider the following code.
<?php
$passMarks = 40;
echo $passMarks >= 40 ? "You passed the exam." : "You failed the exam.";
?>
The echo statement contains the conditional expression that evaluates to the string “You passed the exam.” if the condition is true. If the condition is false, it evaluates to the string “You failed the exam.”. In this way, the above statement with the conditional operator performs the same function as the preceding PHP if-else statement.
Examples of If-Else Statement
Let us take some important examples based on if-else statement that you should practice.
Example 2:
<?php
$username = "admin";
$password = "1234";
if($username == "admin" && $password == "1234") {
echo "Login successful.";
} else {
echo "Invalid username or password.";
}
?>
Output:
Login successful.
Example 3:
<?php
$age = 25;
if ($age >= 18) {
echo "You are eligible to vote.";
} else {
echo "You are not eligible to vote.";
}
?>
Output:
You are eligible to vote.
Advanced Practical Examples for Best Practice
Let’s take an example in which we will take two numbers and check that number is even or odd. For example, if we take a number 30, then it will display “30 is even number.”. If we take 31, it will display “31 is odd number.”.
Example 4:
<?php
$num1 = 30;
$num2 = 35;
if($num1 % 2 == 0) {
echo $num1 . " is an even number." . "\n";
}
else {
echo $num1 . " is an odd number." . "\n";
}
if($num2 % 2 == 0) {
echo $num . " is an even number." . "\n";
}
else {
echo $num2 . " is an odd number." . "\n";
}
?>
Output:
30 is an even number.
35 is an odd number.
Let’s take an example where we will check whether a number is divisible with another number or not and display an appropriate message.
Example 5:
<?php
$num1 = 30;
$num2 = 7;
if($num1 % $num2 == 0) {
echo $num1 . " is divisible by ". $num2;
}
else {
echo $num1 . " is not divisible by " . $num2;
}
?>
Output:
30 is not divisible by 7
Let’s write a program in which we will take a number and check whether it is a buzz number or not. A number is a buzz number when it ends with 7 or is divisible by 7.
Example 6:
<?php
$num = 777;
if(($num % 10 == 0) || ($num % 7 == 0))
echo $num . " is a Buzz number.";
else
echo $num . " is not a Buzz number.";
?>
Output:
777 is a Buzz number.
Let’s write a program in PHP where we will take marks of three subjects and then calculate the total marks, percentage, and grade using if else statement.
Example 8:
<?php
$chem = 95;
$phy = 76;
$maths = 82;
$total = $chem + $phy + $maths;
$per = $total/3;
echo "Total: " .$total. "\n";
echo "Percentage: " .$per . "\n";
if($per >= 80)
echo "Grade A";
else
echo "Grade B";
?>
Output:
Total: 253
Percentage: 84.333333333333
Grade A
Nested If-Else Statement in PHP
When an if-else statement is declared inside another if or if-else statement, it is called a nested if-else statement in PHP. The inner if statement can also contain another if or if-else statement, creating a hierarchical structure of conditions. PHP programmers commonly use nested if-else statements to handle multiple layers of conditions.
Syntax of Nested If-Else Statement
// Outer if statement.
if (condition1) {
// Inner if-else statement declared in outer if else statement.
if (condition2) {
// Code to execute if both condition1 and condition2 are true
} else {
// Code to execute if condition1 is true but condition2 is false
}
// Else part of outer if statement.
} else {
// Code to execute if condition1 is false
}
In the above syntax of nested if-else statement, the first condition-1 will evaluate and if it is true, the inner if-else block will execute. If the condition-1 is false, the else part of outer if statement will execute and the inner if-else block will skip.
Example of Nested If-Else Statement
Example 9:
<?php
$marks = 85;
$attendance = 90;
if ($marks >= 75) {
if ($attendance >= 80) {
echo "You passed with distinction.";
} else {
echo "Your attendance is too low for distinction.";
}
} else {
echo "You did not score enough marks to pass.";
}
?>
Output:
You passed with distinction.
Example 10:
<?php
$age = 20;
$citizenship = "US";
if ($age >= 18) { // First condition
if ($citizenship == "US") { // Nested condition
echo "You are eligible to vote.";
} else {
echo "You must be a US citizen to vote.";
}
} else {
echo "You are not old enough to vote.";
}
?>
Output:
You are eligible to vote.
Best Practices for Using If Else Statement
There are some important points while using if else statement in PHP program that you should keep in mind. They are:
- Always use curly braces even for single-line statements because curly braces improve readability and prevent logical errors.
- Avoid deep nesting because too many nested if-else statements can make the code hard to read.
- Use the logical operators wisely if need.
- For simple conditions, try to use the ternary operator.


