What Is a Control
Structure?
A control structure allows you to control the flow of code execution
in your application.
Generally, a program is executed sequentially, line by line, and a
control structure allows you to alter that flow, usually depending on
certain conditions.
12/10/2024 1
Flowchart - how a control structure
works
12/10/2024 2
Flowchart Explained
• As you can see in the above diagram, first a condition is
checked. If the condition is true, the conditional code
will be executed.
12/10/2024 3
Example of Control Structures
12/10/2024 4
PHP if...else...elseif Statements
• Conditional statements are used to perform different actions
based on different conditions.
• Control statements are conditional statements that execute a block
of statements if the condition is correct. The statement inside the
conditional block will not execute until the condition is satisfied.
12/10/2024 5
PHP Conditional Statements
In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true and another
code if that condition is false
• if...elseif...else statement - executes different codes for more than two
conditions
• switch statement - selects one of many blocks of code to be executed
12/10/2024 6
PHP - The if Statement
• The if statement executes some code if one condition is true.
12/10/2024 7
PHP - The if Statement
• if (condition) {
code to be executed if condition is true;
• The if statement executes some code if one condition is true.
12/10/2024 8
If statement
<?php
$t = 48;
if ($t %2==0) {
echo "The no. is even";
}
?>
12/10/2024 9
Execution flow of the if
statement
12/10/2024 10
PHP - The if...else Statement
The if...else statement executes some code if a condition is true and
another code if that condition is false.
12/10/2024 11
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
12/10/2024 12
Execution flow of the if-else statement
12/10/2024 13
Example
<?php
$t = 49;
if ($t %2==0) {
echo "The no. is even";
}
else
{
echo "The no. is odd";
}
?>
12/10/2024 14
PHP - The if...elseif...else Statement
• The if...elseif...else statement executes different codes for more than
two conditions.
12/10/2024 15
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
12/10/2024 16
<?php
$a = 49;
$b = 48;
$c = 70;
if ($a>$b && $a>$c){
echo "a is greatest";
}
else if ($b>$a && $b>$c)
{
echo"b is greatest";
}
else{
echo"c is greatest";
}
?>
12/10/2024 17
Execution flow of the nested-if-
else statement
12/10/2024 18
PHP switch Statement
• The switch statement is used to perform different actions based on
different conditions.
• Use the switch statement to select one of many blocks of code to be
executed.
12/10/2024 19
The PHP switch Statement
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
12/10/2024 20
How Switch Works
• This is how it works: First we have a single expression n
(most often a variable), that is evaluated once.
• The value of the expression is then compared with the values for
each case in the structure.
• If there is a match, the block of code associated with that case is
executed.
• Use break to prevent the code from running into the next case
automatically.
• The default statement is used if no match is found.
12/10/2024 21
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
12/10/2024 22
Execution flow of the switch
statement
12/10/2024 23
PHP Loops
• Often when you write code, you want the same block of code to run
over and over again a certain number of times.
• So, instead of adding several almost equal code-lines in a script, we
can use loops.
12/10/2024 24
Loops
• Loops are used to execute the same block of code again and again, as
long as a certain condition is true.
12/10/2024 25
In PHP, we have the following loop
types:
• while - loops through a block of code as long as the specified
condition is true
• do...while - loops through a block of code once, and then repeats the
loop as long as the specified condition is true
• for - loops through a block of code a specified number of times
• foreach - loops through a block of code for each element in an array
12/10/2024 26
PHP while Loop
• The while loop - Loops through a block of code as long as the
specified condition is true.
• The while loop executes a block of code as long as the specified
condition is true.
12/10/2024 27
Syntax
while (condition is true) {
code to be executed;
}
12/10/2024 28
The example below displays the numbers from 1 to 5:
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
12/10/2024 29
PHP do while Loop
The do...while loop - Loops through a block of code once, and then
repeats the loop as long as the specified condition is true.
12/10/2024 30
The PHP do...while Loop
• The do...while loop will always execute the block of code once, it will
then check the condition, and repeat the loop while the specified
condition is true.
12/10/2024 31
Syntax
do {
code to be executed;
}
while (condition is true);
12/10/2024 32
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
12/10/2024 33
Do While Loop
• In a do...while loop the condition is tested AFTER executing the
statements within the loop.
• This means that the do...while loop will execute its statements at least
once, even if the condition is false.
12/10/2024 34
PHP for Loop
• The for loop - Loops through a block of code a specified number of
times.
• The for loop is used when you know in advance how many times the
script should run.
12/10/2024 35
Syntax
Syntax
for (initialize counter; test counter; increment/decrement counter) {
code to be executed for each iteration;
}
12/10/2024 36
Parameters:
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the
loop continues. If it evaluates to FALSE, the loop ends.
• increment counter: Increases the loop counter value
12/10/2024 37
Example of for loop
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
12/10/2024 38
Example Explained
• $x = 0; - Initialize the loop counter ($x), and set the
start value to 0
• $x <= 10; - Continue the loop as long as $x is less than
or equal to 10
• $x++ - Increase the loop counter value by 1 for each
iteration
12/10/2024 39
PHP foreach Loop
• The foreach loop - Loops through a block of code for each element in
an array.
• The foreach loop works only on arrays, and is used to loop through
each key/value pair in an array.
12/10/2024 40
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current
array element is assigned to $value and the array
pointer is moved by one, until it reaches the last
array element.
12/10/2024 41
<!DOCTYPE html>
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
</body>
</html>
12/10/2024 42
PHP Break and Continue
PHP Break
• It was used to "jump out" of a switch statement.
• The break statement can also be used to jump out of a loop.
12/10/2024 43
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
12/10/2024 44
PHP Continue
• The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in
the loop.
12/10/2024 45
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
12/10/2024 46
<?php
$x = 0;
while($x < 10) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
$x++;
}
?>
12/10/2024 47
<?php
$x = 0;
while($x < 10) {
if ($x == 4) {
$x++;
continue;
}
echo "The number is: $x <br>";
$x++;
}
?>
12/10/2024 48