Slide 1

Slide 1 text

PHP 101: flow control Fundamental PHP & Web Development lecturer : Lucien Lee 李柏緯 Lecture 2

Slide 2

Slide 2 text

? What is flow control 2

Slide 3

Slide 3 text

3 eat SLEEP DRINK PUPU Straight Life

Slide 4

Slide 4 text

4 Real Life is complicated

Slide 5

Slide 5 text

5 Based Condition, Decide what to do.

Slide 6

Slide 6 text

IF 6 if something happen, what should we do?

Slide 7

Slide 7 text

IF •If my dog is gone, I call its name. 7

Slide 8

Slide 8 text

IF if($dog == ‘gone’){ echo “Where are you, Lucy!”; } 8

Slide 9

Slide 9 text

IF..ELSE.. 9 more statement to if not

Slide 10

Slide 10 text

ELSE •If my dog is gone, I call its name; else I play with it. 10

Slide 11

Slide 11 text

ELSE if($dog == ‘gone’){ echo “Where are you, Lucy!”; }else{ $myState = “play”; } 11

Slide 12

Slide 12 text

ELSEIF 12 Give MOre condition and RESPonse

Slide 13

Slide 13 text

ESLEIF •If my dog is gone, I call its name; else if my dog is eating, I go to eat. 13

Slide 14

Slide 14 text

ELSEIF if($dog == ‘gone’){ echo “Where are you, Lucy!”; }elseif($dog == ‘eating’){ $myState = “go to eat”; } 14

Slide 15

Slide 15 text

? LOOP 15

Slide 16

Slide 16 text

16 eat SLEEP DRINK PUPU But you get older

Slide 17

Slide 17 text

17 for Do one by one, until satisfy condition.

Slide 18

Slide 18 text

18 for •Count from 1 to 10.

Slide 19

Slide 19 text

for for($i=1; $i<=10; $i++){ echo $i.’
’; } 19

Slide 20

Slide 20 text

20 while Do again by again, until satisfy condition.

Slide 21

Slide 21 text

21 while •Count from 1 to 10.

Slide 22

Slide 22 text

while $i=1 while($i<=10){ echo $i.’
’; i++; } 22

Slide 23

Slide 23 text

23 foreach traverse array one by one

Slide 24

Slide 24 text

foreach $price = array(“apple”=>100, ”orange”=>150, “banana”=>200); foreach($price as $key => $value){ echo “水果: ”. $key.”/價錢: ”. $value; } 24

Slide 25

Slide 25 text

ARRAY Part 2 25

Slide 26

Slide 26 text

? Multi-Array 26

Slide 27

Slide 27 text

ARRAY •a collection of data items. •key map to value. •Like many box put together. 27 Apple 100 Orange 150 Banana 200

Slide 28

Slide 28 text

28 0 1 2 0 A B C 1 D E F 2 G H I Multi-Case

Slide 29

Slide 29 text

Multi-Array 29 $array[0][‘cool’] = ‘awesome’; $array[1] = “XD”; $array[0][1][‘yeah’] = “yo”; //wrong case $array[0]=100;

Slide 30

Slide 30 text

key-value 30 red white yellow purple flower rose lily sumflower fruit apple pear banana grape

Slide 31

Slide 31 text

Multi-Array 31 $set=array( ‘flower’=>array(‘red’=>‘rose’,‘whit e’=>‘lily’,‘yellow’=>‘sunflower’), ‘fruit’=>array(‘red’=>‘apple’, ‘white’=>‘pear’,‘yellow’=>‘banana’, ‘purple’=>‘grape’) );

Slide 32

Slide 32 text

+ union two array 32

Slide 33

Slide 33 text

two array plus $item1 = array(‘rose’,‘lily’); $item2 = array(‘apple’,‘banana’,‘grape’); $items = $item1 + $item2; //$items = array(‘rose’,‘lily’, ‘grape’); 33

Slide 34

Slide 34 text

array_push() add a new data to array 34

Slide 35

Slide 35 text

array_pop() remove last data from array 35

Slide 36

Slide 36 text

Live Coding 36

Slide 37

Slide 37 text

HomeWork 37 • make key-value array to save student grade. • All grade * 1.4, if modified grade over 100, modify in another way:original value + (100 - original value)*0.1 • change array as name map to array save grade and pass or not. a b c d e f 30 40 90 60 55 80