Ephrem Tesfaye - IP-II - Chapter-Three
Ephrem Tesfaye - IP-II - Chapter-Three
Topic Contents
Control structures are the structures within a language that allow you to control the flow of
execution through a program or script. You can group them into conditional (or branching)
structures and repetition structures (or loops). If you want to sensibly respond to your users’ input,
your code needs to be able to make decisions. The constructs that tell your program to make
decisions are called conditionals.
For many of the decisions you make, you have more than two options. You can create a sequence
of many options using the elseif statement, which is a combination of an else and an if statement.
When you provide a sequence of conditions, the program can check each until it finds one that is
true. Note that you are free to type elseif or else if—versions with or without a space are both
correct.
Example:
<?php
$t = date("H");
if($t <"10") {
echo"Have a good morning!";
}elseif($t <"20") {
echo"Have a good day!";
}else{
echo"Have a good night!";
}
?>
3.1.2 Switch statement
The switch statement works in a similar way to the if statement, but it allows the condition to take
more than two values. In an if statement, the condition can be either true or false. In a switch
statement, the condition can take any number of different values, as long as it evaluates to a simple
type (integer, string, or float). You need to provide a case statement to handle each value you want
to react to and, optionally, a default case to handle any that you do not provide a specific case
statement for.
<?php
$favcolor="red";
switch($favcolor){
case"red":echo"Your favorite color is red!"; break;
case"blue":echo"Your favorite color is blue!"; break;
default:echo"Your favorite color is neither red, blue!";
}
?>
The switch statement behaves somewhat differently from an if or elseif statement. An if statement
affects only one statement unless you deliberately use curly braces to create a block of statements.
A switch statement behaves in the opposite way. When a case statement in a switch is activated,
PHP executes statements until it reaches a break statement. Without break statements, a switch
would execute all the code following the case that was true. When a break statement is reached,
the next line of code after the switch statement is executed.
3.2 Looping statements
If you need something done the same way a number of times, you can use a loop to repeat some
parts of your program. Loop statements tell PHP to execute a statement or block repeatedly.
3.2.1 While statement
The simplest kind of loop in PHP is the while loop. Like an if statement, it relies on a condition.
The difference between a while loop and an if statement is that an if statement executes the code
that follows it only once if the condition is true. A while loop executes the block repeatedly for as
long as the condition is true. You generally use a while loop when you don’t know how many
iterations will be required to make the condition true. If you require a fixed number of iterations,
consider using a for loop.
The basic structure of a while loop is:
while( condition ) {
expressions
};
At the beginning of each iteration, the condition is tested. If the condition is false, the block will
not be executed and the loop will end. The next statement after the loop will then be executed.
Example:
<?php
$x =0;
while($x <=100) {
echo"The number is: $x <br>";$x+=10;
}
?>
Activity 1
Write a PHP code to generate HTML table with 50 rows, by just using while loop.
Example:
<?php
$x =6;
do{
echo"The number is: $x <br>";
$x++;
}while($x <=5);
?>
3.2.3 for loop and foreach loop
You can write this style of loop in a more compact form by using a for loop. The basic structure
of a for loop is:
for( expression1; condition; expression2){
expression3;
}
Expression1 is executed once at the start. Here, you usually set the initial value of a counter.
The condition expression is tested before each iteration. If the expression returns false,
iteration stops. Here, you usually test the counter against a limit.
Expression2 is executed at the end of each iteration. Here, you usually adjust the value of
the counter.
Expression3 is executed once per iteration. This expression is usually a block of code and
contains the bulk of the loop code.
Example:
<?php
for($x =0; $x <=100; $x+=10){
echo"The number is: $x <br>";
}
?>
As well as the for loop, there is a foreach loop, designed specifically for use with arrays. It is used
to loop through each key/value pair in an array.
Example:
<?php
$age = array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x => $val) {
echo"$x = $val<br>";
}
?>
3.3 Break and continue statements
If you want to stop executing a piece of code, you can choose from three approaches, depending
on the effect you are trying to achieve.
If you want to stop executing a loop, you can use the break statement as previously
discussed in the section on switch. If you use the break statement in a loop, execution of
the script will continue at the next line of the script after the loop.
If you want to jump to the next loop iteration, you can instead use the continue statement.
If you want to finish executing the entire PHP script, you can use exit. This approach is
typically useful when you are performing error checking.
Example 1:
<?php
for($x =0; $x <10; $x++) {
if($x ==4) {
continue;
}
echo"The number is: $x <br>";}
?>
Example 2:
<?php
for($x =0; $x <10; $x++) {
if($x ==4) {
break;
}
echo"The number is: $x <br>"
;
}?>