5.control StatementsFinal
5.control StatementsFinal
Structure?
A control structure allows you to control the flow of code execution
in your application.
12/10/2024 1
Flowchart - how a control structure
works
12/10/2024 2
Flowchart Explained
12/10/2024 3
Example of Control Structures
12/10/2024 4
PHP if...else...elseif Statements
12/10/2024 5
PHP Conditional Statements
In PHP we have the following conditional statements:
12/10/2024 6
PHP - The if Statement
12/10/2024 7
PHP - The if Statement
• if (condition) {
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
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
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
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:
12/10/2024 26
PHP while Loop
12/10/2024 27
Syntax
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
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:
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
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
<?php
$colors = array("red", "green", "blue", "yellow");
</body>
</html>
12/10/2024 42
PHP Break and Continue
PHP Break
• It was used to "jump out" of a switch statement.
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
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;
12/10/2024 47
<?php
$x = 0;
12/10/2024 48