Conditional Statements are used to execute different blocks of code based on whether a condition is true or false.
In PHP programming language, the most common conditional statements are:
The if else statement is used to execute one block of code if a condition is true and another block of code if the condition is false.
When the condition is true, the first block of code is executed. When the condition is false, the second block of code is executed.
The computer can be said to be lazy. It starts at the top and keeps checking until it finds a "TRUE" answer. Once it finds one, it does that action and ignores everything else below it.
In PHP programming language, the syntax for an if else statement is as follows:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
NOTE: For a statement with only the if part, nothing happens when the condition is false.
Comparators are used to compare values. The most common comparators are:
To illustrate the use of if else statements, let's write a simple program. We will create a space on the memory to store an integer value and determine whether it is even or odd.
The algorithm is as follows:
In PHP programming language, the code for this algorithm would look like this:
<?php
$num = 4;
if($num%2==0){
echo "<p>".$num." is even</p>";
}else{
echo "<p>".$num." is odd</p>";
}
?>
In the above program, the condition being checked is whether the number is divisible by 2 using the modulo operator (%). If the result of $num % 2 is 0, it means the number is even; otherwise, it is odd.
Note: The modulo operator (%) returns the remainder of a division operation. If a number is divisible by 2, the remainder will be 0, indicating that the number is even. Otherwise, the number is odd.
Another program could be a program that stores the age of a person and determines if they are eligible to vote (assuming the voting age is 18).
The algorithm is as follows:
The code for this algorithm would look like this:
<?php
$age = 17;
if($age >= 18){
echo "<p>You are legible to vote</p>";
}else{
echo "<p>You are not eligible to vote</p>";
}
?>
The if else statement is used to check multiple conditions and execute different blocks of code based on which condition is true.
The syntax for an if else statement is as follows:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else if (condition3) {
// code to be executed if condition3 is true
}
.
.
.
else if (conditionN) {
// code to be executed if conditionN is true
} else {
// code to be executed if none of the conditions are true
}
The number of the else if clauses depends on the number of conditions to be checked.
Since the first is if and the last is else, the number of else if can be calculated as:
Number of else if clauses = Number of conditions - 2
For instance if there are 5 posible conditions, there will be 3 (5 - 2) else if clauses.
Let's use the B.E.C.E grading system as an example:
| Score | Grade |
| 75% - 100% | 1 |
| 70% - 74% | 2 |
| 65% - 69% | 3 |
| 60% - 64% | 4 |
| 55% - 59% | 5 |
| 50% - 54% | 6 |
| 45% - 49% | 7 |
| 40% - 44% | 8 |
| 0% - 39% | 9 |
There are 9 options and hence 7 (9 - 2) else if clauses.
To write a program that stores score on the memory and displays the corresponding grade:
The algorithm is as follows:
The code for this algorithm would look like this:
<?php
$score = 68;
if ($score >= 75) {
echo "<p>Your grade is 1</p>";
} else if ($score >= 70) {
echo "<p>Your grade is 2</p>";
} else if ($score >= 65) {
echo "<p>Your grade is 3</p>";
} else if ($score >= 60) {
echo "<p>Your grade is 4</p>";
} else if ($score >= 55) {
echo "<p>Your grade is 5</p>";
} else if ($score >= 50) {
echo "<p>Your grade is 6</p>";
} else if ($score >= 45) {
echo "<p>Your grade is 7</p>";
} else if ($score >= 40) {
echo "<p>Your grade is 8</p>";
} else {
echo "<p>Your grade is 9</p>";
}
?>
Nested if else statements are if else statements that are contained within another if else statement. They allow you to check multiple conditions in a hierarchical manner.
We can use a nested if else statement to implement the grade-checking algorithm more efficiently.
We will this time check if the score value is within a specific range before determining the grade
The algorithm is as follows:
The modified code for this algorithm would look like this:
<?php
$score = 105;
if ($score < 0 || $score > 100) {
echo "<p>Invalid score. Please enter a score between 0 and 100</p>";
}else{
if ($score >= 75) {
echo "<p>Your grade is 1</p>";
} else if ($score >= 70) {
echo "<p>Your grade is 2</p>";
} else if ($score >= 65) {
echo "<p>Your grade is 3</p>";
} else if ($score >= 60) {
echo "<p>Your grade is 4</p>";
} else if ($score >= 55) {
echo "<p>Your grade is 5</p>";
} else if ($score >= 50) {
echo "<p>Your grade is 6</p>";
} else if ($score >= 45) {
echo "<p>Your grade is 7</p>";
} else if ($score >= 40) {
echo "<p>Your grade is 8</p>";
} else {
echo "<p>Your grade is 9</p>";
}
}
?>
The grade checking if else is the nested if-else structure.
We could also modify the voting system to ensure the age value isn't a negative number using the nested if-else structure.
The modified code for this would look like this:
<?php
$age = -17;
if($age < 0){
echo "<p>Please age can't be negative</p>";
}else{
if($age >= 18){
echo "<p>You are legible to vote</p>";
}else{
echo "<p>You are not eligible to vote</p>";
}
}
?>
The switch statement is used to execute different blocks of code based on the value of a variable or expression. It is an alternative to using multiple if else statements when you have a large number of conditions to check.
The syntax for a switch statement is as follows:
switch (variable) {
case value1:
// code block
break;
case value2:
// code block
break;
.
.
.
case valuen:
// code block
break;
default:
// code block
break;
}
The switch statement evaluates the variable and compares it with each case label. When a match is found, the corresponding code block is executed. The break statement is used to exit the switch statement after a match is found and block of code executed.
The default case is optional and is executed if none of the case labels match the variable.
The default is similar to else.
The break; makes it to exit the switch since a match is found.
Without the break;, the execution will continue until next break; is found or the entire rest of code is executed.
We can use the switch alternative to write the code for the even number check.
The code for this would look like this:
<?php
$num = 7;
switch($num%2){
case 0:
echo "<p>".$num." is even</p>";
break;
default:
echo "<p>".$num." is odd</p>";
break;
}
?>
In the above code, the expressing $num%2 returns either 0 or 1 which is compared to the cases (0 and default).
When it is 0, the block of code after the case 0: is executed and any other remainder, the default block of code is executed.
Another example of the switch statement would be to store integer value from 1 - 7 to represent the days of the week.
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday
7 - Sunday
The code for this would look like this:
<?php
$day = 5;
switch($day){
case 1:
echo "<p>Monday</p>";
break;
case 2:
echo "<p>Tuesday</p>";
break;
case 3:
echo "<p>Wednesday</p>";
break;
case 4:
echo "<p>Thursday</p>";
break;
case 5:
echo "<p>Friday</p>";
break;
case 6:
echo "<p>Saturday</p>";
break;
case 7:
echo "<p>Sunday</p>";
break;
default:
echo "<p>Invalid day</p>";
break;
}
?>
Note: the break statements are crucial to prevent fall-through to subsequent cases.
When the break is omitted, the program will continue to execute the next case, leading to unintended behavior.