Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views7 pages

Ephrem Tesfaye - IP-II - Chapter-Three

Chapter Three of Internet Programming II covers flow controls in PHP, including branching statements like if and switch, as well as looping statements such as while, do...while, for, and foreach. It explains how to use break and continue statements to manage loop execution and provides examples for each control structure. The chapter aims to equip students with the skills to control program flow and solve problems using PHP.

Uploaded by

herid72018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Ephrem Tesfaye - IP-II - Chapter-Three

Chapter Three of Internet Programming II covers flow controls in PHP, including branching statements like if and switch, as well as looping statements such as while, do...while, for, and foreach. It explains how to use break and continue statements to manage loop execution and provides examples for each control structure. The chapter aims to equip students with the skills to control program flow and solve problems using PHP.

Uploaded by

herid72018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Internet Programming II Chapter Three - PHP Flow Controls

College of Engineering, Technology and Computational Sciences


Department of Computer Science

Course Title: Internet Programming II (COSC 3032)


Topic: Flow Controls in PHP

Topic Contents

3. Flow Controls in PHP


3.1 Branching statements / Conditional statements
3.1.1 If statements
3.1.2 Switch statement
3.2 Looping statements
3.2.1 While statement
3.2.2 Do … while statement
3.2.3 for loop and foreach loop
3.3 Break and continue statements
Objectives
After studying this chapter, you should be able to:
 Understand how to control the program’s flow
 Checking conditions in PHP
 Decisions in PHP
 Understand and use types of branching and looping statements in PHP
 Understand and use break and continue statements in loops, and conditions
 Solve different problems involving branching and looping flows
 Use PHP to write simple working and problem solving browser programs

Compiled By: Ephrem Tesfaye Tsidu 1


Internet Programming II Chapter Three - PHP Flow Controls

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.

3.1 Branching statements / Conditional statements


3.1.1 If statements
You can use an if statement to make a decision. You should give the if statement a condition to
use. If the condition is true, the following block of code will be executed. Conditions in if
statements must be surrounded by parentheses ().
Often you may have more than one statement you want executed according to the actions of a
conditional statement such as if. You can group a number of statements together as a block. To
declare a block, you enclose it in curly braces.
Example:
<?php
$t = date("H”);
if ($t <" 20"){
echo "Have a good day!”;
}
?>
An else statement allows you to define an alternative action to be taken when the condition in an
if statement is false.
Example:
<?php
$t = date("H");
if($t <"20") {
echo"Have a good day!";
}else{
echo"Have a good night!";
}
?>

Compiled By: Ephrem Tesfaye Tsidu 2


Internet Programming II Chapter Three - PHP Flow Controls

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!";
}
?>

Compiled By: Ephrem Tesfaye Tsidu 3


Internet Programming II Chapter Three - PHP Flow Controls

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.

Compiled By: Ephrem Tesfaye Tsidu 4


Internet Programming II Chapter Three - PHP Flow Controls

3.2.2 Do … while statement


A do...while loop differs from a while loop because the condition is tested at the end. This means
that in a do...while loop, the statement or block within the loop is always executed at least once.
The general structure of a do...while statement is:
Do{
expressions;}
while( condition );

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:

Compiled By: Ephrem Tesfaye Tsidu 5


Internet Programming II Chapter Three - PHP Flow Controls

<?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>";}
?>

Compiled By: Ephrem Tesfaye Tsidu 6


Internet Programming II Chapter Three - PHP Flow Controls

Example 2:
<?php
for($x =0; $x <10; $x++) {
if($x ==4) {
break;
}
echo"The number is: $x <br>"
;
}?>

_______________The End! _______________

Compiled By: Ephrem Tesfaye Tsidu 7

You might also like