Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML 213 0151 291 3113.

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML 213 0151 291 3113."— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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:

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE PHP if if ($money == $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); }

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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!

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE if else if ($money == $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); } else { echo (“Incorrect fare”); }

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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!

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution 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

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE IF and Multiple Expressions So far we have seen examples of IF testing one condition. What if you wanted to check two?

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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?

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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%

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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”); } }

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Alternatively if ($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”); }

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Testing for NULL if (!empty($yourVariable)) { // do something! }

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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; }

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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.

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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.

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SWITCH Syntax switch ($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):

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE while The basic structure of a while loop is similar to an IF while (condition) { // code goes here }

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE while $option = 9; while ($option != 0) { echo (“My simple menu”); echo (“1. Launch Excel ”); echo (“2. Launch Word ”); echo (“0. Exit Menu”); }

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE do…while do { echo (“1. Launch Excel ”); echo (“2. Launch Word ”); echo (“0. Exit Menu”); } while ($option != 0);

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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));

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for The FOR loop happens a predetermined amount of times. For loops can be nested

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Simple for for ($counter = 1 ; $counter < 11 ; $counter ++) { echo (“Loop ”.$counter); }

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE foreach $myArray[] = “jim”; $myArray[] = “bo”; echo (“ ”); foreach ($myArray as $temp) { echo (" ".$temp." “); } echo (“ ”); For your assessment you will need create arrays and step through them in order to prepare the data for storage in the database

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Nesting Loops, regardless of which loop you are using can be put inside a loop. This is called a nested loop.

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Nested FOR for ($counter = 1 ; $counter < 11 ; $counter ++) { echo (“Loop ”.$counter. “ ”); for ($incounter = 1 ; $incounter<6 ; $incounter ++) { echo (“Inside Loop ”.$incounter. “ ” ); } Loop 1 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 Loop 2 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 Loop 3 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 Loop 4 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 and soforth.......

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE WARNING! Be very careful when using loops Especially when nesting loops!

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What happens here? for ($counter = 1;$countre < 11;$counter ++) { echo (“Loop ”.$counter); }

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What happens here? for ($counter=1;$counter<11;$counter++) { for ($counter=1;$counter <6 ; $counter ++) { echo (“Inside Loop ”.$counter. “ ” ); }

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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

39 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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).

40 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE PHP IF if ($money = $fare) { echo (“Fare ok”) echo (“sit down and enjoy the ride”) }

41 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE if else if ($money = $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”) } else { echo (Incorrect fare); }

42 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE WHILE $option = 9; while (option != 0) echo (“My simple menu”); echo (“1. Launch Excel ”); echo (“2. Launch Word ”); echo (“0. Exit Menu”) }

43 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Testing for NULL if (!empty(yourVariable) { // do something! } otherwise { // do something else! }

44 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Simple FOR for ($counter = 1, $counter < 11, $counter++) { echo (“Loop ”.$counter);

45 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE foreach $myArray[] = “jim”; $myArray[] = bo; for each ($myArray = $temp) { echo (" ".$temp." “); }

46 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 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


Download ppt "Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML 213 0151 291 3113."

Similar presentations


Ads by Google