If Statement in PHP
The if statement is one of the most straightforward conditional statement in PHP. It is the simplest decision-making construct that allows you to control the flow of a program by specifying an alternative path of execution.
This statement executes a block of statements or code when the specified condition is true, and skips the block if the condition is false.
If statement is also called conditional control statement or single selection statement in PHP because it selects or ignores a single statement or a single group of statements.
Syntax of if Statement in PHP
The if statement in PHP consists of a Boolean expression (i.e. condition) followed by one or more statements. The general syntax for using the if statement is as follows:
if (condition) {
// Code to be executed if the condition is true
}In the above syntax, the test condition consists of a Boolean expression that returns a Boolean value, either true or false. It must be enclosed within parentheses ( () ).
If the test condition (i.e. expression) inside the parentheses evaluates to true, the block of code (also called body of if statement) executes. The block of code is skipped or bypassed if the test condition is false, .
If the body of if statement contains only a single statement, curly braces ({}) are optional but it is strongly recommended to include them to improve readability and prevent errors.
Flowchart Diagram of if Statement in PHP
Here is a flowchart diagram illustrating the ‘if statement’ in PHP.
Let’s take a simple example to understand the single selection if statement.
Example 1:
<?php
$myPer = 92;
if($myPer >= 80) {
echo "Grade A";
}
?>
In this example, if the Boolean expression (i.e., condition) evaluates to true, the statement or code inside the curly braces ({ }) will execute, and the message “Grade A” will print on the browser. After the statement inside the if block executes, the program will continue with the immediate next statement in the sequence.
If the Boolean expression evaluates to false, the code inside the if statement will skip, and the program will move directly to the immediate next statement in the sequence.
Example 2:
<?php
if(true)
echo "Code to be executed."; // print.
if(false)
echo "Code not to be executed."; // not print.
?>
Output:
Code to be executed.
Basic Examples of If Statement
Let’s take some basic examples based on the if statement that you should practice.
Example 3:
<?php
$x = 1;
if($x > 0)
echo $x . " is a positive number.";
echo "\n";
$y = 1;
if($y) // same as: if(y != 0)
echo $y . " is a nonzero number.";
echo "\n";
$a = 10;
$b = 10;
if($a == $b)
echo "Both are equal numbers.";
?>
Output:
1 is a positive number.
1 is a nonzero number.
Both are equal numbers.
Example 4:
<?php
$age = 18;
if($age >= 18) {
echo "You are eligible to vote.";
}
?>
Output:
You are eligible to vote.
In this example, the test condition ($age >= 18) evaluates to true, so the code inside the if block executes and displays the output on the console.
Example 5:
<?php
$radius = 2;
$pi = 3.14;
if($radius >= 0) {
$area = $radius * $radius * $pi;
echo "Area of circle: " . $area;
}
?>
Output:
Area of circle: 12.56
In the above code, we have calculated the area of circle. Since the test condition ($radius >= 0) evaluates to true, so statements inside the block of if statement will execute.
Use of Logical Operators in If Statement
You can also use PHP logical operators in the conditional expression when you need to check multiple conditions together. Let us take some examples based on logical operators used in the if statement.
Example 6:
<?php
$x = 20;
$y = 40;
$z = 50;
if(($y > $x) AND ($y < $z)) // true
echo "The value of y is greater than the value of x but smaller than the value of z.";
echo "\n";
if(($x > $y) AND ($y < $z)) // false
echo "The value of z is greater than value of x and y.";
echo "\n";
if($y % $x == 0 AND $x != 0) // true
echo "The value of y is divisible by the value of x.";
?>
Output:
The value of y is greater than the value of x but smaller than the value of z.
The value of y is divisible by the value of x.
In this example, the first and third if statements evaluates true as both conditional expressions joined by AND operator are true. The second if statement evaluates false, as the first conditional expression is false.
The if statement will produce true only if both expressions joined by AND operator are true. If any of the expressions is false or both expressions are false, the if statement will give false.
Example 7:
<?php
$x = 2;
$y = 1;
$z = 4;
// Case 1
if ($value = ($x > $y) OR ($y < $z)) {
echo $value . "\n";
}
// Case 2
if ($value = ($x > $y) OR ($y > $z)) {
echo $value . "\n";
}
// Case 3
if ($value = ($x < $y) OR ($y < $z)) {
echo $value . "\n";
}
// Case 4
if ($value = ($x < $y) OR ($y > $z)) {
echo $value;
}
?>
Output:
1
1
1. Operator Precedence:
- The OR operator has lower precedence than the assignment (=) operator.
- This means $value = ($x > $y) OR ($y < $z) is parsed as:
($value = ($x > $y)) OR ($y < $z);
Here, $value is first assigned the result of the expression ($x > $y).
2. Evaluating each case:
- Case 1: $value = ($x > $y) OR ($y < $z)
- $x > $y is true (2 > 1).
- $value = true. The second part ($y < $z) is evaluated but doesn’t affect $value.
- Output: 1 (PHP prints true as 1).
- Case 2: $value = ($x > $y) OR ($y > $z)
- $x > $y is true (2 > 1).
- $value = true. The second part ($y > $z) is evaluated but doesn’t affect $value.
- Output: 1.
- Case 3: $value = ($x < $y) OR ($y < $z)
- $x < $y is false (2 < 1).
- $value = false. The second part ($y < $z) is evaluated but doesn’t affect $value.
- No output because $value is false.
- Case 4: $value = ($x < $y) OR ($y > $z)
- $x < $y is false (2 < 1).
- $value = false. The second part ($y > $z) is evaluated but doesn’t affect $value.
- No output because $value is false.
Example 8:
<?php
$x = 2;
$y = 1;
if($value = ($x == 2) && ($y != 2))
echo $value;
?>
Output:
1
1. Operator Precedence:
- In PHP, the && operator (logical AND) has a higher precedence than the assignment operator (=).
- This means the condition ($x == 2) && ($y != 2) is evaluated first, and the result (true or false) is then assigned to $value.
2. Condition Evaluation:
- $x == 2 evaluates to true because 2 == 2.
- $y != 2 evaluates to true because 1 != 2.
The logical AND (&&) operator combines these conditions. Since both conditions are true, the result of ($x == 2) && ($y != 2) is true.
3. Assignment and Output:
- The result of the condition (true) is assigned to the variable $value.
- Since the value of variable $value is now true, the if statement is satisfied, and the echo statement executes.
- In PHP, the boolean value (true) is printed as 1.
Nested If Statements
Sometimes, you may need to check multiple conditions by nesting if statements. When you use another if statement inside an existing if statement to check another condition within an existing condition, it is called nested if statements in PHP. This structure allows you to evaluate multiple related conditions in a hierarchical manner. Let’s take a simple example on it.
Example 9:
<?php
$age = 20;
$citizenship = "US";
if ($age >= 18) { // first condition
if ($citizenship == "US") { // nested condition
echo "You are eligible to vote.";
}
}
?>
Output:
You are eligible to vote.
Here, the second if statement will execute only if the test condition of the first if statement is true. Otherwise, the second if statement will not execute.
Common Errors with If Statements
There are so many common errors while using if statement in PHP code that beginners do. They are:
- Missing parentheses
- Using assignment operator (=) instead of comparison operator (==).
- Ignoring curly braces for multiple statements.





