PHP Part 4: Control Flow Statements In PHP

PHP PART 4: CONTROL FLOW STATEMENTS IN PHP


Please Subscribe Youtube| Like Facebook | Follow Twitter

Control Flow Statements In PHP

In this article, we will provide a detailed overview of control flow statements in PHP and provide examples of how they are used in PHP programming.

Control flow statements are used in programming to control the flow of code execution. In PHP, there are several types of control flow statements, including if-else statement, switch statement, for loop, while loop, and do-while loop. In this article, we’ll go over each of these statements and provide examples to help you understand their usage.

If-else statement

If-else statements are used to execute different code based on different conditions. Here is the syntax for an if-else statement:

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

In an if-else statement, the code inside the first set of curly braces is executed if the condition is true. If the condition is false, the code inside the second set of curly braces is executed.

In some cases, you may need to evaluate multiple conditions. In these cases, you can use the else if statement. Here is the syntax for multiple if-else statement:

if (condition1) {
  // code to be executed if condition1 is true
} elseif (condition2) {
  // code to be executed if condition2 is true
} else {
  // code to be executed if all conditions are false
}

The else if statement allows you to evaluate multiple conditions and execute different blocks of code based on the result of the evaluations

Here’s an example of if-else statement in PHP

<?php
$num = 10;

if ($num < 5) {
  echo "The number is less than 5.";
} elseif ($num == 5) {
  echo "The number is equal to 5.";
} else {
  echo "The number is greater than 5.";
}
?>

Output

The number is greater than 5.

In this example, the variable $num is assigned the value of 10. The if statement checks whether $num is less than 5. Since it’s not, the elseif statement checks whether $num is equal to 5. Since it’s not, the else statement is executed, which outputs “The number is greater than 5.”

SWITCH STATEMENT

Switch statements are used to execute different code based on different values. Here is the syntax for a switch statement:

switch (expression) {
  case value1:
    // code to be executed if expression is equal to value1
    break;
  case value2:
    // code to be executed if expression is equal to value2
    break;
  default:
    // code to be executed if expression is not equal to any of the values
}

In a switch statement, the expression is evaluated and compared to each case value. If the expression is equal to a case value, the code inside the corresponding set of curly braces is executed. If the expression is not equal to any of the case values, the code inside the default set of curly braces is executed.

Here’s an example of a switch statement in PHP:

<?php
$dayOfWeek = 'Tuesday';

switch ($dayOfWeek) {
  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 'It is the weekend';
}
?>

Output

Today is Tuesday

In this example, the variable $dayOfWeek is assigned the value of ‘Tuesday’. The switch statement checks the value of $dayOfWeek against different cases. Since the value is ‘Tuesday’, the second case is executed and “Today is Tuesday” is output. If $dayOfWeek had a different value, the default case would be executed and “It is the weekend” would be output.

FOR LOOP

For loops are used to repeat a block of code a specific number of times. Here is the syntax for a for loop:

for (initialization; condition; increment) {
  // code to be executed
}

In a for loop, the initialization sets the starting point, the condition determines when to stop, and the increment increases the counter. The code inside the curly braces is executed each time the loop runs.

Here’s an example of a for loop in PHP:

<?php
for ($i = 1; $i <= 10; $i++) {
  echo $i . " ";
}
?>

Output

1 2 3 4 5 6 7 8 9 10

In this example, the for loop is used to output the numbers from 1 to 10. The loop initializes a counter variable $i to 1, specifies a condition that $i should be less than or equal to 10, and increments $i by 1 on each iteration. On each iteration, the loop body echoes the value of $i followed by a space. The loop continues until the condition $i <= 10 is no longer true, at which point the loop terminates.

Break and Continue Statements Inside a For Loop

continue and break are flow control statements used in loops to alter the normal control flow of the program.

The continue statement is used to skip the current iteration of a loop and continue to the next iteration. This is useful when you want to skip processing certain items in a loop based on a specific condition.

On the other hand, the break statement is used to immediately terminate the current loop and continue with the rest of the program outside the loop. This is useful when you want to exit a loop prematurely based on a specific condition.

Both continue and break can be used in for, while, and do-while loops to alter the normal flow of control.

Here is an example of using break and continue statements inside a for loop in PHP:

<?php

for ($i = 1; $i <= 10; $i++) {
    if ($i == 3) {
        continue;
    }
    if ($i == 5) {
        break;
    }
    echo $i . "<br>";
}

?>

Output

1
2
4

In this example, the loop iterates from 1 to 10. When i is 3, the continue statement is encountered, so the remaining statements in that iteration are skipped and the loop proceeds to the next iteration. When i is 5, the break statement is encountered and the loop is terminated early. So only the values 1, 2, and 4 are printed to the output.

WHILE LOOP

While loops are used to repeat a block of code as long as a condition is true. Here is the syntax for a while loop:

while (condition) {
  // code to be executed
}

In a while loop, the condition is evaluated at the beginning of each iteration. If the condition is true, the code inside the curly braces is executed. This continues until the condition becomes false.

Here’s an example of a while loop in PHP:

<?php
$count = 1;
while ($count <= 5) {
  echo $count . " ";
  $count++;
}
?>

Output

1 2 3 4 5

In this example, the while loop is used to output the numbers from 1 to 5. The loop initializes a counter variable $count to 1, and then executes the loop body as long as the condition $count <= 5 is true. On each iteration, the loop body echoes the value of $count followed by a space, and increments $count by 1. The loop continues until the condition $count <= 5 is no longer true, at which point the loop terminates.

DO-WHILE LOOP

Do-while loops are similar to while loops, but the code inside the curly braces is executed at least once, even if the condition is false. Here is the syntax for a do-while loop:

do {
  // code to be executed
} while (condition);

In a do-while loop, the code inside the curly braces is executed first, and then the condition is evaluated. If the condition is true, the loop continues to run. If the condition is false, the loop ends.

Here’s an example of a do-while loop in PHP:

<?php
$count = 1;
do {
  echo $count . " ";
  $count++;
} while ($count <= 5);
?>

Output

1 2 3 4 5

In this example, the do-while loop is used to output the numbers from 1 to 5. The loop initializes a counter variable $count to 1, and then executes the loop body at least once. On each iteration, the loop body echoes the value of $count followed by a space, and increments $count by 1. After each iteration, the loop condition $count <= 5 is checked. If the condition is true, the loop continues to execute. If the condition is false, the loop terminates. In this case, the loop executes 5 times, because the condition $count <= 5 is true for the values of $count from 1 to 5.

Conclusion

In conclusion, PHP offers several control flow statements that allow you to execute code based on certain conditions or to iterate over a block of code multiple times. The if-else statement allows you to execute code based on a condition, while the switch statement allows you to select from a set of values. Loops, such as the for, while, and do-while loops, allow you to execute a block of code multiple times, either for a specific number of iterations or as long as a condition is true.

By using these control flow statements, you can create more dynamic and flexible code that can respond to different situations and handle varying amounts of data.

PHP Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *