PHP Programming

Conditional (Selection) Statements

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:

  1. if statement: Executes a block of code if a specified condition is true.
  2. else statement: Executes a block of code if the condition in the if statement is false.
  3. else if statement: Specifies a new condition to test if the previous condition was false.
  4. switch statement: Allows you to execute different blocks of code based on the value of a variable or expression.

The If Else Statement

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

Comparators are used to compare values. The most common comparators are:

  1. ==: Equal to
  2. !=: Not equal to
  3. >: Greater than
  4. <: Less than
  5. >=: Greater than or equal to
  6. <=: Less than or equal to

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:

  1. Start.
  2. Create space and store integer value.
  3. Check if the number is divisible by 2.
  4. If the number is divisible by 2, display "The number is even".
  5. If the number is not divisible by 2, display "The number is odd".
  6. End.

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:

  1. Start.
  2. Create space and store the age value.
  3. Check if the age is greater than or equal to 18.
  4. If the age is greater than or equal to 18, display "You are eligible to vote."
  5. If the age is less than 18, display "You are not eligible to vote."
  6. End.

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

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:

  1. Start.
  2. Create space and store the score value.
  3. Check if the score is greater than or equal to 75 . If true, display "Your grade is 1".
  4. Check if the score is greater than or equal to 70. If true, display "Your grade is 2".
  5. Check if the score is greater than or equal to 65. If true, display "Your grade is 3".
  6. Check if the score is greater than or equal to 60. If true, display "Your grade is 4".
  7. Check if the score is greater than or equal to 55. If true, display "Your grade is 5".
  8. Check if the score is greater than or equal to 50. If true, display "Your grade is 6".
  9. Check if the score is greater than or equal to 45. If true, display "Your grade is 7".
  10. Check if the score is greater than or equal to 40. If true, display "Your grade is 8".
  11. If none of the above conditions are true, display "Your grade is 9".
  12. End.

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

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:

  1. Start.
  2. Create and store score value.
  3. Check if the score is between 0 and 100. If false, display "Invalid score. Please enter a score between 0 and 100." and end the program.
  4. If the score is valid, check if the score is greater than or equal to 75 . If true, display "Your grade is 1".
  5. Check if the score is greater than or equal to 70. If true, display "Your grade is 2".
  6. Check if the score is greater than or equal to 65. If true, display "Your grade is 3".
  7. Check if the score is greater than or equal to 60. If true, display "Your grade is 4".
  8. Check if the score is greater than or equal to 55. If true, display "Your grade is 5".
  9. Check if the score is greater than or equal to 50. If true, display "Your grade is 6".
  10. Check if the score is greater than or equal to 45. If true, display "Your grade is 7".
  11. Check if the score is greater than or equal to 40. If true, display "Your grade is 8".
  12. If none of the above conditions are true, display "Your grade is 9".
  13. End.

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

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;
}

Explanation

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.