Control Statements

Control Statements are statements that control the flow of code in any PHP program and decides which code will be executed when and in which situation will not be Execute. PHP also has many Statements which contain code Flow is used to control. We know that any program is a set of statements, which in general are written in the Source File. They are Execute in the same order. Execution of Statements written in a program, in which order is called, is called Flow of control.
Types of Control Statement or Control Structure in php
In PHP, we can divide different types of Conditional Statements in two main sections.

  1. Conditional Statements.
  2. Loop Statements.

Conditional Statements

These are the following types.

  1. if statement
  2. if-else statement
  3. switch statement

if statement

If statement is a control statement used to test a particular condition. The condition is only executed once the condition is true.And if conditions are false then condition is not execute.Its syntex has been given below.

Syntex Example
if(condition){

// if condition true, statement of here will be executed

}

if(6<8){

// execute condition here because condition is true

}

if-else Statement

If-else statement is used to test a particular condition. If statement is true then if statement is executed, else condition is executed if condition is false.

Switch Statement

This statement is also a selection statement that defines different paths for the execution of a program. The switch statement is a multi-way branch statement. The switch statement is the same as if statement. There are cases in the switch statement. You give a choice. The case of the switch which matches the choice, becomes the same execute. When no case matches, the default case becomes execute.

Loop Statements

These are the following types.

  1. While Loop
  2. Do-While Loop
  3. For Loop
  4. For Each Loop

While Loop

while loop executes the statements until the condition remains true. When the execution of the statement of the statement is stopped when the condition is false. You give a condition to control the loop. The value of that condition changes with every loop, and then it changes. Unless the condition becomes false. If you do not increase the condition variable, the condition will never be false and the execution of the loop will continue.

Syntax

while (condition is true) {
code to be executed;
}

Example

<?php 
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
} 
?>
output//

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

Do-While loop

Do while loop is the same as in the loop just before the statements are executed and condition is checked later. Do while loop statements are initially executed even if the condition is true or false. This is because condition in the do while loop is checked later.

Syntax

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

Example

<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
The number is: 1 
The number is: 2 
The number is: 3 
The number is: 4 
The number is: 5 

For Loop

For loop is a very simple loop. In this loop you initialize the variables that control the loop, type the condition and increment the variable, and all of these are in one place.

Syntax

for (init counter; test counter; increment counter) {
code to be executed;
}

Example

<?php 
for ($x = 1; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
The number is: 1 
The number is: 2 
The number is: 3 
The number is: 4 
The number is: 5 
The number is: 6 
The number is: 7 
The number is: 8 
The number is: 9 
The number is: 10

For Each Loop

For each loop arrays, it’s designed to be iterate. These loops only work with arrays. If you use it with variables, then this error shows. With For each loop, you can easily perform operations on array elements.

Syntax

foreach ($array as $value) {
code to be executed;
}

Example

<?php
$name = array("jyoti", "rahul", "sonam", "khusi");
foreach ($name as $value) {
echo "$value <br>";
}
?>
output///

jyoti
rahul
sonam
khusi