Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decisions, Loops, and Arrays Achmad Arwan, S.Kom.

Similar presentations


Presentation on theme: "Decisions, Loops, and Arrays Achmad Arwan, S.Kom."— Presentation transcript:

1 Decisions, Loops, and Arrays Achmad Arwan, S.Kom

2 IF Statements In PHP we have the following conditional statements: if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false if...elseif....else statement - use this statement to select one of several blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed

3 The IF Statements The if Statement Use the if statement to execute some code only if a specified condition is true. Syntax if (condition) code to be executed if condition is true;

4 The if...else Statement Use the if....else statement to execute some code if a condition is true and another code if a condition is false. if (condition) code to be executed if condition is true; else code to be executed if condition is false;

5 The if...else Statement If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces: "; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?>

6 The if...elseif....else Statement Use the if....elseif...else statement to select one of several blocks of code to be executed. if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false;

7 The PHP Switch Statement Use the switch statement to select one of many blocks of code to be executed. switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; }

8 PHP Loops In PHP, we have the following looping statements: while - loops through a block of code while a specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true for - loops through a block of code a specified number of times foreach - loops through a block of code for each element in an array

9 The while Loop The while loop executes a block of code while a condition is true. Syntax while (condition) { code to be executed; } "; $i++; } ?>

10 The do...while Statement The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true. Syntax do { code to be executed; } while (condition); "; } while ($i

11 The for Loop The for loop is used when you know in advance how many times the script should run. Syntax for (init; condition; increment) { code to be executed; } Parameters: init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop) condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration) "; } ?>

12 The foreach Loop The foreach loop is used to loop through arrays. Syntax foreach ($array as $value) { code to be executed; } For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value. "; } ?>

13 Arrays An array is a set of variables that all have the same name but a different index. Each member of the array is called an element. Each element's name must be followed by its index in square brackets: $nama[1]=“Adi”; $nama[2]=“Dina”;

14 Initialization of Arrays $author[0]="William Shakespeare"; $author = array ("William Shakespeare", "Franz Kafka"); $author = array (1=>"William Shakespeare", "Franz Kafka"); $author = array (“wl”=>"William Shakespeare", “Fk”=>"Franz Kafka");

15 Iterating through an Array for ($counter=1; $counter $author[$counter]"; } Or $counter = 1; while ($counter < 51) { echo" $author[$counter]"; $counter = $counter + 1; }

16 current() and key() Functions PHP uses a pointer to keep track of which element it's at when it moves through an array. The pointer indicates the element that is currently being used by the script. You can use the current() function to view the value of that element. you can use the key() function to find out its index value. (Key is another name for index.)

17 current() and key() sample $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur"; $current_index_value = key($director); echo ($current_index_value); $current_contents = current($director); echo ($current_contents);

18 next() and prev() Functions These functions enable you to navigate through arrays by moving the pointer to the next or previous element in the array. next()  move to next array previous()  move to previous array

19 next() and prev() Functions $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur"; $director[]="Alfred Hitchcock"; next($director); $current_index_value = key($director); echo ($current_index_value); prev($director); $current_index_value = key($director); echo ($current_index_value);

20 list() and each() Functions use the list() and each() functions to return only the elements in the array that contain data. Syntax while (list($element_index_value, $element_contents) = each($director)) { echo " $element_index_value - $element_contents"; }

21 list() and each() Functions sample $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur"; while (list($MickeyMouse, $DonaldDuck) = each ($Director)) { echo " $MickeyMouse - $DonaldDuck"; }

22 sort() function sort() is the most basic of the sorting functions. It takes the contents of the array and sorts them into alphabetical order. The function requires only an array name to sort the array: sort(ArrayName)

23 sort() sample <?php $director = array ("Orson Welles","Carol Reed", "Fritz Lang", "Jacques Tourneur"); echo “Before sort”; while (list($IndexValue, $DirectorName) = each ($Director)){ echo " $IndexValue - $DirectorName"; } sort($director); echo “Before sort”; while (list($IndexValue, $DirectorName) = each ($Director)){ echo " $IndexValue - $DirectorName"; } ?>

24 Multidimentional array TitlePriceNumber rose1.2515 daisy0.7525 orchid1.157 "rose", Price => 1.25, Number => 15 ), array( Title => "daisy", Price => 0.75, Number => 25, ), array( Title => "orchid", Price => 1.15, Number => 7 ) ); ?>

25 Multidimentional Arrays Manual access to each element "; echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]." "; echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]." "; echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]." "; echo " Using loops to display array elements "; echo " "; for ($row = 0; $row The row number $row "; echo " "; for ($col = 0; $col ".$shop[$row][$col]." "; } echo " "; echo " "; } echo " "; ?>

26 <?php echo “Thanks”; ?>


Download ppt "Decisions, Loops, and Arrays Achmad Arwan, S.Kom."

Similar presentations


Ads by Google