Types of Control Statements in PHP
- Conditional/Selection statements
- Iteration/Loop statements
- Jump statements
1. Conditional statements in PHP
There are two basic types of the first kind of Control Statement in PHP(conditional statements) in any programming language,
- IF, ELSE, and ELSEIF Statements
- SWITCH Statement
The if, elseif, and else Statement in PHP
This section will discuss one of the two mentioned conditional statements: the if, elseif, and else statements.
The if statements are used in cases where the condition is to be checked for a range of values. The syntax for the same looks like this,
if (condition) {
code to be executed if true;
} elseif (condition) {
code to be executed if the first condition is false and the second condition is true;
} else {
code to be executed in all other cases;
}
Let us look at an example and see the functioning of the code,
PHP
<?php
$t = date("d");
echo("Today is " . $t . "\n");
if ($t < "10") {
echo "Starting of the month";
} elseif ($t < "20") {
echo "It's the mid of the month";
} else {
echo "The month is almost about to end";
}
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Today is : 09
Starting of the month
We fetched the current date and displayed different messages based on other dates in the above code. If the current date is less than 10, in that case, the first condition gets executed. If the date is less than 20 but greater than 10, then the second condition is performed, and the third and final condition is implemented in all other cases.
It is voluntary to use if, elseif, and else together. The if the keyword can be used independently, although to use else and elseif keywords, we need the if statement to be present.
For example, This is a valid code.
PHP
<?php
$t = date("d");
echo("Today is : " . $t . "\n");
if ($t < "10") {
echo "Starting of the month";
}
echo("\n")
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Today is : 09
Starting of the month
But the following code will throw an error since there is no if statement present before the elseif statement.
PHP
<?php
$t = date("d");
echo("Today is : " . $t . "\n");
elseif ($t < "10") {
echo "Starting of the month";
}
echo("\n")
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
ERROR!
PHP Parse error: syntax error, unexpected token "elseif", expecting end of file in /tmp/HR6X8r6bju.php on line 4
Switch Statement in PHP
Contrary to the if statement, the switch statement is used when the condition contains a particular value rather than a range of values. The syntax for the switch statement in any coding language looks like this,
switch (n) {
case value1:
code to be executed if n=value1;
break;
case value2:
code to be executed if n=value2;
break;
case value3:
code to be executed if n=value3;
break;
...
default:
code to be executed if no other cases are satisfied;
}
Let us look at this code, where the current day is printed using the switch statement. The compiler checks the different cases and then prints the present-day accordingly.
PHP
<?php
$t = date("D");
echo $t ."\n";
switch ($t) {
case "Sun":
echo "Hey, Today is Sunday!";
break;
case "Mon":
echo "Hey, Today is Monday!";
break;
case "Tue":
echo "Hey, Today is Tuesday!";
break;
case "Wed":
echo "Hey, Today is Wednesday!";
break;
case "Thu":
echo "Hey, Today is Thursday!";
break;
case "Fri":
echo "Hey, Today is Friday!";
break;
default:
echo "Hey, Today is Saturday!";
}
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Sun
Hey, Today is Sunday!
2. Loop Statements in PHP
Loop statements are another part of the Control Statement in PHP. When we want to run a snippet of code repeatedly for a certain number of times, we use loop statements instead of adding similar code lines in a script and making the program more complex. Until the condition remains true, loops run the block of code present inside it.
PHP languages have these four loops for this -
While Loop
The block of code written inside the while loop will get executed until the given condition is true.
Syntax-
while ( given condition is true) {
the block of code that needs to be executed;
}
Example-
PHP
<?php
echo " Coding ninja \n";
$x = 1;
while($x <= 7) {
echo "The current number is: $x \n";
$x++;
}
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Coding ninja
The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5
The current number is: 6
The current number is: 7
In the code $x = 1, we initialize the loop counter ($x) and set the start value to 1. Furthermore, the code $x <= 7 continues the Loop as long as $x is less than or equal to 7. At last, the loop counter value by 1 for each iteration is increased by $x++.
Do while Loop
This app is slightly different from the while loop as this Loop, without checking any condition, executes the block of code once then runs it. At the same time, the given condition gives true value. which means the do-while Loop executes the block of code once even if the condition is false.
Syntax-
do {
the code snippet that needs to be executed;
} while (condition is true);
Example-
PHP
<?php
echo " Coding ninja \n";
$x = 1;
do {
echo "The number is: $x \n";
$x++;
} while ($x <= 5);
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Coding ninja
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The following example first sets the variable $x to 1 ($x = 1). The do-while loop will print some output and increment the variable $x by one. The condition is then tested (is $x less than or equal to 5? ), and the Loop will continue to execute as long as $x is less than or equal to 5:
For Loop
When we are sure about the number of times we want to run the specific block of code, we put that code inside for Loop, specifying the conditions.
Syntax-
for (init counter; test counter; increment counter) {
the code to be executed for each iteration;
}
Here the loop counter value is initialized in the init counterpart. Furthermore, in the test counterpart, we check the condition for each loop iteration. If true, it will continue executing; otherwise, end the Loop. And in the increment counter, part of the loop counter value is changed (increased or decreased).
Example-
PHP
<?php
echo " Coding ninja \n";
for ($x = 0; $x <= 5; $x++) {
echo "The number is: $x \n";
}
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Coding ninja
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Here the counter variable is initialized by setting its value to 0. Then the condition is provided that the loop will continue executing the block of code if the value of the counter variable ($x) is less than or equal to 5. With each iteration, the counter variable value will increase by 1.
Foreach Loop
When we want to run a block of code for each element in an array, we use foreach loop.
Syntax of PHP foreach loop
foreach ($array as $value) {
code to be executed;
}
Example of PHP foreach loop -
PHP
<?php
echo " Coding ninja \n";
$colors = array("red", "violet", "blue", "purple");
foreach ($colors as $value) {
echo "$value \n";
}
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Coding ninja
red
violet
blue
purple
The $value will take the value of the current array element for each loop iteration, and the array pointer is moved by one unless it reaches the array's last element.
3. Jump statements in PHP
Break
For jumping out from the loop, we use the break statement. The execution of the for, while, do-while, switch, and for-each loop is broken by keyword break. This statement breaks the current flow of the loop at the specified condition and then continues with the following line of code outside the loop.
PHP
<?php
echo " Coding ninja \n";
for ($x = 0; $x < 10; $x++) {
if ($x == 6) {
break;
}
echo "The number is: $x \n";
}
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Coding ninja
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
This loop will break when the value of the x becomes 6.
Continue
This statement is different from the break statement. It breaks the loops or skips the execution of code only for the specified condition, continues with the next iteration of the loop, and continues the loop/ current flow of the program.
PHP
<?php
echo "Coding ninja \n";
for ($x = 0; $x < 10; $x++) {
if ($x == 2) {
continue;
}
echo "The number is: $x \n";
}
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Coding ninja
The number is: 0
The number is: 1
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
This code skips the execution of code when the value of x is 2.
Must Read PHP Projects With Source Code
Frequently Asked Questions
Why do we use control statements?
Control statements are used in programming to make decisions, control the flow of execution based on certain conditions, and manage loops or iterations. They enable developers to write flexible, dynamic, and efficient code.
What are the types of control statements in PHP?
For controlling the flow of the program, the control statements are beneficial for the flow of execution of statements. The Control Statements in PHP are divided into three types which are Conditional/Selection statements, Iteration/Loop statements, and Jump statements.
What is the syntax for control structure in PHP?
The control statements are used for running the code based on some conditions. They control the flow of execution of statements. The opening brace should be replaced with a colon (:), and the ending brace like endif;.
Why is control statements in PHP used?
When a condition is met, a block of statements known as a control statement is carried out. In PHP, we employ control statements such as if-else statements or loops such as while or do-while loops.
What are the controls used in PHP?
In PHP, control structures include if, else, elseif, switch for decision-making, and for, foreach, while, do-while for looping. These constructs allow developers to control the flow of execution based on specific conditions or repeat code blocks.
Conclusion
In this article, we talked about Control Statements in PHP, which are essential for controlling the flow of program execution. We discussed if, if-else, and switch statements for conditional branching, and for, while, and do-while loops for repetitive tasks. These Control Statements in PHP provide the necessary tools to make decisions, repeat code blocks, and build robust and dynamic web applications.