Control Statements in PHP: Decision-Making and Switch Statement

What are Control Statements in PHP

In PHP, control statements are used to control the flow of execution in a program.

It allows you to make decisions, execute code repeatedly, and perform other types of control over your PHP scripts.

Control Statements allow users to choose the order in which a program’s instructions are executed, and allow a program to perform specific actions, perform different tasks repeatedly, or even switch from one piece of code to another.

What is the flow control statement in PHP?

Flow Control Statements in PHP are used to manage the execution flow of a program.

They allow programmers to control how code is executed based on certain conditions or loops.

Flow control statements include:

  • Loop Statements:
    – are used to execute a block of code repeatedly as long as a specified condition is true.

There are several types of Loop statements:

  1. while Loop
  2. do…while Loop
  3. for Loop
  4. foreach Loop

Here’s one example of “for” loop statements in PHP:

<?php
// for loop
echo "For loop counting from 1 to 5:<br>";
for ($i = 1; $i <= 5; $i++) {
    echo "$i ";
}
?>

The “for” loop starts with initializing the loop variable $i to 1. It then checks the condition $i <= 5, and if true, executes the loop body.

After each iteration, it increments the loop variable $i” by 1 (“$i++“).

The loop continues until the condition $i <= 5 becomes false.

Each iteration of the loop prints the value of “$i“, effectively counting from 1 to 5.

Output:

For loop counting from 1 to 5:
1 2 3 4 5

  • Conditional Statements:
    – are used to execute different blocks of code based on certain conditions and allow the program to make decisions and choose different paths of execution.

Here are the main types of Conditional Statements:

  1. if Statement
  2. if else Statement
  3. if elseif else Statement
  4. switch Statement

Here’s one example of if elsestatements in PHP:

<?php
$grades = 78;

// If...else statement
if ($grades> 75) {
    echo "You Passed!!";
} else {
    echo "You Failed!!";
}
?>

We have a variable $grades set to 78.

The if statement checks if the grade is greater than 75. If true, it prints “You Passed!!” If false, it executes the else block and prints “You Failed!!.”

Output:

You Passed!!

Additional types of flow control statements

  • Jump Statements:
    – are used to alter the normal flow of execution within a program, and the statements allow programmers to transfer control from one part of the code to another.

There are several types of Jump Statements:

  1. break Statement
  2. continue Statement
  3. return Statement

Here’s one example of breakstatements within the “for” loops in PHP:

<?php
// Using break statement in a for loop
for ($i = 1; $i <= 10; $i++) {
    if ($i == 6) {
        break; // Exit the loop when $i equals 6
    }
    echo "$i ";
}
?>

We have a “for” loop that iterates from 1 to 10.

Inside the loop, there’s an “if” condition checking if the loop variable “$i” equals 6.

If “$i” equals 6, the “break” statement is executed, which terminates the loop prematurely.

As a result, only the numbers from 1 to 5 are printed, and the loop exits when “$i” becomes 6.

Output:

1 2 3 4 5

What is Decision-Making in PHP?

Decision-making in PHP is a method that allows programmers to create options or decisions using conditional statements.

For decision statements to work, the programmer must also define one or more conditions that the program tests or evaluates.

If the condition is true, the conditional statements are executed. However, if the condition is false, other statements are sometimes executed.

Consider carefully the flow of the following simple decision statement.

Different types of decision-making statements in PHP?

In this section, we will discuss the different types of decision statements.

Programming in PHP, allows you to make decisions using the following conditional statements:

  • if Statement
  • if else Statement
  • if elseif else Statement
  • switch Statement

PHP allows you to perform actions based on logical and comparison conditions.

Furthermore, the result of these circumstances (TRUE or FALSE) will be the action requested by the user.

Therefore, a conditional statement works like a bidirectional path: when a specific result is desired, the program is executed based on the statement. otherwise, the opposite happens.

To define the different types of decision-making in PHP, let’s start with…

1. if Statement

The PHP if statement is the simplest form of a conditional statement used in a program.

The code snippet is only executed if the condition is true.

Additionally, the if statement allows programmers to specify an action based on a specific condition.

The PHP IF statement syntax is provided below for your reference.

Syntax:

if (condition) {
    // code to execute if condition is true
}

Example:

<?php
$age = 20;
if ($age >= 18) {
    echo "You are an adult.";
}

?>

Output:

You are an adult.

This PHP code checks if the variable $age” has a value equal to or greater than 18.

If it does, it prints “You are an adult.” to the screen. Since$age is set to 20, and the message is printed.

However, the program will not display any results if the condition is false.

Here’s an Example of a Flowchart using an “if” statement.

2. if…else Statement

The if statement is intended as a condition with an assigned function and supporting action if the result is true.

However, if the result is false, the program will not print anything unless you add the else statement.

Making if…else decisions in PHP allows programmers to have two executable blocks of code that handle true or false results.

Syntax:

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

Example:

<?php
$temperature = 25;
if ($temperature > 20) {
    echo "It's warm outside.";
} else {
    echo "It's cold outside.";
}

?>

Output:

It's warm outside.

This PHP code sets the variable “$temperature” to “25” and then checks if it’s greater than “20“.

If “$temperature” is indeed greater than “20″, it prints “It’s warm outside.”; otherwise, it prints “It’s cold outside.”.

Since “$temperature” is “25“, the output will be “It’s warm outside.“.

Here’s an Example of a Flowchart using an “if else” statement.

3. if…elseif…else Statement

Another condition in PHP is the elseif statement, which allows developers to attach multiple conditions to an if…else statement.

To clarify, you can use the else statement (after if) to describe a block of code that will only be executed if the first condition is false.

However, if the first test condition is false, use elseif to specify another test condition.

Syntax:

if (condition1) {
    // code to execute if condition1 is true
} elseif (condition2) {
    // code to execute if condition2 is true
} else {
    // code to execute if neither condition1 nor condition2 is true
}

Example:

<?php
$grade = 75;
if ($grade >= 90) {
    echo "A";
} elseif ($grade >= 80) {
    echo "B";
} elseif ($grade >= 70) {
    echo "C";
} else {
    echo "D";
}
?>

Output:

C

This PHP code assigns the value 75 to the variable $grade. It then checks the value of $grade against different conditions:

  • If $grade is greater than or equal to 90, it prints “A”.
  • If not, but $grade is greater than or equal to 80, it prints “B”.
  • If not, but $grade is greater than or equal to 70, it prints “C”.
  • If none of the above conditions are met, it prints “D”.

Since $grade is 75, it satisfies the condition for “C”, so the output will be “C”.

Here’s an example Flowchart using an “if elseif else” statement.

4. switch Statement

The Switch statement executes multiple cases that match the condition and executes the case block accordingly.

Evaluate the expression before comparing each case’s value. Therefore, every time a case is matched, an identical case is processed.

However, to use the switch statement in PHP decision-making, you need to know two different terms:

  • break – tells the program to ignore the switch-case statement block after executing the first true case code.
  • default – used when the value of the constant expression never matches the value of the expression. If there are no standard statements or case matches, no statements in the body of the switch will be executed.

Syntax:

switch (expression) {
    case value1:
        // code to execute if expression equals value1
        break;
    case value2:
        // code to execute if expression equals value2
        break;
    case value3:
        // code to execute if expression equals value3
        break;
    case value4:
        // code to execute if expression equals value4
        break;
    case value5:
        // code to execute if expression equals value5
        break;
    default:
        // code to execute if expression does not match any case
}

Example:

<?php 
$day = "Monday";
switch ($day) {
    case "Monday":
        echo "Today is Monday.";
        break;
    case "Tuesday":
        echo "Today is Tuesday.";
        break;
    case "Wednesday":
        echo "Today is Wednesday.";
        break;
    case "Thursday":
        echo "Today is Thursday.";
        break;
    case "Friday":
        echo "Today is Friday.";
        break;        
    default:
        echo "Today is not Monday or Friday.";
}

?>

Output:

Today is Monday.

This PHP code initializes the variable $day with the value “Monday” and then uses a switch statement to check the value of $day. 

Depending on the value of $day, it prints a corresponding message.

Since $day is “Monday“, the output will be “Today is Monday.“.

Here’s an Example of a Flowchart using a “switch” statement.

Conclusion

In summary, the topic “Control Statements in PHP” covers all the conditional statements used in PHP programming.

Control Statements in PHP help you apply programming logic and develop your skills to make good decisions and options.

I hope you’ve learned something new in this discussion. 

If you have questions or inquiries please don’t hesitate to comment below.

Leave a Comment