if, else, elseif php
In PHP program development, if, else, elseif, and conditional statements are used to execute or test multiple blocks of program source code simultaneously to determine whether a particular logic or condition is true or false. In PHP program development, if, else, and elseif statements are crucial for producing program decisions in multiple blocks.

If Statement in PHP.
The if statement in PHP tests any given program condition logic in the current program. If the program condition given in the current if block is true, it prints the corresponding block statement to the console screen.
Syntax of the if statement.
if (condition) {
//run the if statement source code only when the condition is true
}
Example of an if statement.
<?php
$voter_age = 21;
if ($voter_age >= 20) {
echo “You are able to vote”;
}
?>
Explanation of the if statement.
Here, if $voter_age in the if statement is greater than or equal to 20, it will print the message “You are able to vote” on the console screen.
else statement in PHP.
In a PHP program, the else statement is displayed and executed after the if statement. Here, the else statement contains the program source code that will execute only if the if statement condition logic is false.
Syntax of the else statement.
if (condition) {
// here the if condition source code to execute if only when the if condition is true
} else {
// here the else statement source code to execute only when the if condition is false
}
Example of the else statement.
<?php
$test_age = 15;
if ($test_age >= 21) {
echo “You are a mature”;
} else {
echo “You are a not mature”;
}
?>
Explanation of the else statement.
Here, in the else statement, $test_age is less than 15 years old. This will execute the else block statement and print the statement “You are a not mature” on the console screen.
elseif statement in PHP.
The elseif statement in PHP helps programmers test multiple program condition logic in multiple sequences. If the elseif statement’s condition logic is false, PHP checks the next elseif condition. If the elseif condition given here is also false, it checks all the elseif statements in the program and finally tests the else block statement and prints the output.
The syntax of the elseif statement.
if (condition) {
// here it program source code is executed only here if condition is true
} elseif (next elseif_condition) {
// here it only executes once above if code to execute if the first if condition is false, but here next elseif_condition is true
} else {
// finally, if above if and elseif both condition false it run code
}
Example of an elseif statement.
<?php
$test_age = 21;
if ($test_age < 11) {
echo “You are kids”;
} elseif ($test_age >= 11 && $test_age < 21) {
echo “You are a minor child”;
} else {
echo “You are mature”;
}
?>
Explanation of the elseif statement.
- Here in the elseif statement first PHP checks if $test_age is less than 11 years. Since $test_age is 21, the if statement condition is false.
- Next, it checks whether $test_age is between 11 and 21 in the elseif statement. This elseif condition is also false.
- Finally, it executes the else block statement and prints the statement “You are mature.”
Multiple elseif conditions in PHP.
In a PHP program, programmers can Analyze or test multiple elseif statement conditions to test the possibility of multiple program statements before finally returning to the else block statement.
Example of multiple elseif conditions.
<?php
$marks = 95;
if ($marks >= 90) {
echo “Grade Allotted – A”;
} elseif ($marks >= 85) {
echo “Grade Allotted – B”;
} elseif ($marks >= 75) {
echo “Grade Allotted – C”;
} elseif ($marks >= 65) {
echo “Grade Allotted – D”;
} else {
echo “Grade – F”;
}
?>
Explanation of multiple elseif conditions.
The PHP program source code here evaluates student marks and prints a grade message based on the student’s marks. Since $marks is 95, the first elseif condition ($$marks >= 90) is true, and it prints the message “Grade Allotted – A”.
Nested if statements in PHP.
In PHP program development, programmers can create if statements, sub-if statements, and source code blocks within each other to make complex decisions. This is called nested or nested statement in PHP program development.
Example of nested if statements.
<?php
$idividual_age = 21;
$permited_License = true;
if ($idividual_age >= 20) {
if ($permited_License) {
echo “You are eligible to drive”;
} else {
echo “You need permission to drive”;
}
} else {
echo “You are not eligible to drive”;
}
?>
Explanation of nested if statements.
Here, in the multiple if PHP program, the program first checks whether $idividual_age is greater than or equal to 20. Because $individual_age is 21, it checks the second condition nested within the nested if statement, which checks whether the individual has a driving license ($permitted_License).
If the individual has a permitted license, it prints the message “You are eligible to drive” to the console. If not, it prints the message “You need permission to drive” to the console.
Abbreviated if ternary operator in PHP.
In PHP program development, the if else statement provides programmers with a detailed version of the ternary operator. The ternary operator is used when programmers need to make a simple decision in a PHP program and indicate a value based on a particular condition.
The syntax of the abbreviated if (ternary operator).
(condition) ? (value_if_true) : (value_if_false);
Example of the abbreviated if (ternary operator).
<?php
$individual_age = 21;
echo ($individual_age >= 20) ? “you are Adult” : “you are not adult”;
?>
A brief explanation of the if (ternary operator).
Here in this program, if $individual_age is greater than or equal to 20, then it prints the message “you are Adult.” Otherwise, it prints the message “you are not adult.”
Explanation of if, else, elseif php Conditional Statements
| Statement | if, else, elseif statement Description | Example or syntax |
| If statement | It Executes a block of program source code only if statement condition is true. | if ($emp_age >= 21) { … } |
| Else statement | It Executes a block of program source code only when the if condition in if is false order. | else { … } |
| elseif (or else if) statement | It enables you to Allows testing multiple additional conditions if the previous conditions are in false state. | elseif ($emp_age < 20) { … } |
| Logical operators | You ca use logical operator to Combine or join multiple if else elseif conditions (e.g., &&, ) |

