www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
PHP Flow Control
Website DevelopmentStewart BlakewayFML [email protected] 291 3113
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Aims• How to use an IF statement• How to execute alternative code if the IF
condition is not true• Nested IF• The SWITCH statement• The WHILE statement• The FOR statement• Nesting Loops
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
PHP Statements
• PHP statements follow the conventions of standard programming languages
• Each line is executed one by one. Starting from the top and working down
• Sometimes you may want to repeat a section of code or ignore a section completely
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Switching Flow
Sometimes depending on a result you will want to switch from executing (doing) one thing to another. We have seen this in JAVASCRIPT. If validation of the form fails, return false…. Otherwise return true
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
if
• We control the flow of execution (events) by checking ‘something’, if something equals the correct thing, we do this. If however, it does not equal what we expect we can do an alternative thing.
• This is called a conditional statement
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
if• We use if everyday! When you get a bus:
If you have the correct fare, pay, take bus. If you don’t have the correct fare, give what you have.
If you have given less than the fare price… get off the bus!If you have given more than the fare price, then you are given change and you take the bus.
• IF is usually broken down. For example:
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
PHP if
if ($money == $fare)
{
echo (“Fare ok”);
echo (“sit down and enjoy the ride”);
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
PHP if else
else
{
echo (“Incorrect fare”);
}
• You can not use an ELSE without an IF…. Otherwise how will the parser know what else you are talking about!
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
if elseif ($money == $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); }else { echo (“Incorrect fare”); }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Not good enough!
• Did you give too much or too little money?
• Work in pairs to write the full PHP solution to resolve the initial question and the above question. Obviously if you gave too much money you should ride the bus. Otherwise you should get back what pittance you gave and get off the bus!
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
So
luti
on if ($money == $fare)
{ echo (“Fare ok”); echo (“sit down and enjoy the ride”); }else { echo (“Incorrect fare”); if ($money > $fare) { $returnmoney = $money-$fare; echo (“Your change sir, enjoy the ride”);
} else { $returnmoney = $money; echo (“get off my bus!”);
} }
This is only one solution.
There could be alternative
solutions that are equally correct
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
IF and Multiple Expressions
• So far we have seen examples of IF testing one condition. What if you wanted to check two?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Multiple Expressions
if (($money == $fare) && ($destination == True)) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); }else { echo (“Incorrect fare”); }
Question
Although this will pretty much work. There is an obvious flaw. Can
you spot it?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Problem!• You work in a car park, the charge system is
fairly simple. If the person leaving has been parked on the car park for less than an hour you charge a flat rate of £1.00.
• If the person has been on the car park for over an hour you charge £2.00.
• This is on the assumption that the person is not a regular (member).
• Members get a discount of 25%
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Variables
• In my solution 3 variables have been identified
– assume $current_time holds the current time– assume $timeonticket holds the time of arrival– assume $person[‘member’] holds the status of if
the person is a member
• Next put the variables to use and write the PHP to solve the problem
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
else { if ($current_time < ($timeonticket + 1hour)) { echo (“£1.00 please sir”);
} else { echo (“£2.00 please sir”); } }
Solution
if ($person[‘member’] == “yes”) { if ($current_time < ($timeonticket + 1hour)) { echo (“75p please sir”);
} else { echo (“£1.50 please sir”); } }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Alternativelyif ($current_time < ($timeonticket + 1hour))
{
if ($person == $member)
{
echo (“75p please sir”);
}
else
{
echo (“£1.00 please sir”);
}
}
else
{
if ($person == $member)
{
echo (“£1.50 please sir”);
}
else
{
echo (“£2.00 please sir”);
}
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Testing for NULL
if (!empty($yourVariable))
{
// do something!
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Summary of if
• More often than not if the statement is not true you will want to do something else. IF ELSE
• Sometimes, a simple IF will suffice, for example: – IF tea too strong, add more milk! Otherwise do
nothing!
if ($tea_strength > $desired_strength)
{
$tea=$tea + $milk;
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Question• What would you do if you wanted to check
more than a couple of responses from a single IF statement?
• For example:– If the person is under 5, let them in free.– If the person is between 6 – 16 charge child fare.– If the person is 16 – 60 charge adult fare.– If the person is 61+ charge OAP fare.
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
switch
• Switch is an ideal solution for more than true or false responses from a single question.
• It uses the case statement.– Case can be though of as: in this case do this.
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
SWITCH Syntaxswitch ($age) { case ($age<5): echo (“Go in free child”); break; case ($age>5 && $age<16): echo (“Please pay child fare”); break; case ($age>16 && $age<60): echo (“Please pay adult fare”); break; case ($age>60): echo (“Please pay OAP fare”); break; }
Question
Although this will pretty much work. There is an obvious flaw. Can
you spot it?
you can also set a default case. If none of the
conditions are met the default will be executed
default (condition):
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What next?• Program control can also be manipulated
with the use of loops.• There are Two basic loops. WHILE and FOR• WHILE loops are usually used when the
predetermined number of iterations is not known
• FOR loops are usually used when the number of iterations is known
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
while
• The basic structure of a while loop is similar to an IF
while (condition) { // code goes here }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
while
$option = 9;
while ($option != 0)
{
echo (“My simple menu”);
echo (“1. Launch Excel<br>”);
echo (“2. Launch Word<br>”);
echo (“0. Exit Menu”);
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
do…while
do
{ echo (“1. Launch Excel<br>”); echo (“2. Launch Word<br>”); echo (“0. Exit Menu”);
}
while ($option != 0);
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Multiple Conditions
• What if you want to check more than one expression?
• Remember the operators?– ||– &&– xor xor – true if either
value evaluates true, but not both
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Multiple Conditions
$age = 19;
$curtime = time();
$exptime = ($curtime + (60 * 60));
while (($age => 18) && ($exptime>$curtime))
{
$curtime = time();
} What conditions must be met in order for the loop to execute?
When will the loop stop executing?
This loop will execute for 1 hour!
Only if your age is 18 or higher
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Multiple Conditions$age = 19;
$curtime = time();
$exptime = ($curtime + (60 * 60));
do
{
echo (“This loop will execute at least once”);
echo (“regardless of the age and the time”);
$curtime = time();
} while (($age => 18) && ($exptime>$curtime));
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for
• The FOR loop happens a predetermined amount of times.
• For loops can be nested
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Simple forfor ($counter = 1 ; $counter < 11 ; $counter ++)
{
echo (“Loop ”.$counter);
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
foreach
$myArray[] = “jim”;
$myArray[] = “bo”;
echo (“<ul>”);
foreach ($myArray as $temp)
{
echo ("<li>".$temp."</li>“);
}
echo (“</ul>”);
For your assessment you will need create
arrays and step through them in order to prepare the data for storage in the database
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Nesting
• Loops, regardless of which loop you are using can be put inside a loop. This is called a nested loop.
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Nested FORfor ($counter = 1 ; $counter < 11 ; $counter ++)
{
echo (“Loop ” .$counter. “<br />”);
for ($incounter = 1 ; $incounter<6 ; $incounter ++)
{
echo (“Inside Loop ” .$incounter . “<br />”);
}
}
Loop 1Inside Loop 1Inside Loop 2Inside Loop 3Inside Loop 4Inside Loop 5Loop 2Inside Loop 1Inside Loop 2Inside Loop 3Inside Loop 4Inside Loop 5Loop 3Inside Loop 1Inside Loop 2Inside Loop 3Inside Loop 4Inside Loop 5Loop 4Inside Loop 1Inside Loop 2Inside Loop 3Inside Loop 4Inside Loop 5
and soforth.......
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
WARNING!
• Be very careful when using loops• Especially when nesting loops!
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What happens here?
for ($counter = 1;$countre < 11;$counter ++)
{
echo (“Loop ”.$counter);
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What happens here?
for ($counter=1;$counter<11;$counter++)
{
for ($counter=1;$counter <6 ; $counter ++)
{
echo (“Inside Loop ”.$counter.“<br />”);
}
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What have we covered?• How to use an IF statement• How to execute alternative code if the IF is
not true• Nesting IFs• The SWITCH statement• The WHILE statement• The FOR statement• Nesting Loops
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Debugging
• Being able to spot syntax mistakes is vital for the exam.
• Over the next couple of slides see if you can spot the mistakes (3 per slide).
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
PHP IF
if ($money = $fare)
{
echo (“Fare ok”)
echo (“sit down and enjoy the ride”)
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
if elseif ($money = $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”) }else { echo (Incorrect fare); }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
WHILE
$option = 9;
while (option != 0)
echo (“My simple menu”);
echo (“1. Launch Excel<br />”);
echo (“2. Launch Word<br />”);
echo (“0. Exit Menu”)
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Testing for NULLif (!empty(yourVariable) { // do something! }otherwise {
// do something else!}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Simple FORfor ($counter = 1 , $counter < 11 , $counter++)
{
echo (“Loop ”.$counter);
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
foreach
$myArray[] = “jim”;
$myArray[] = bo;
for each ($myArray = $temp)
{
echo ("<li>".$temp."</li>“);
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any Questions?
• Make sure you keep up with the seminar exercises. They are designed to prepare you for your assessment
• You should complete the full set each week