11. Associative Arrays An associative array, each ID key is associated with a value. $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34";
13. Multidimensional Arrays In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) );
16. do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
17. for - loops through a block of code a specified number of times
18.
19. 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. <?php$i=1;do { $i++; echo "The number is " . $i . "<br />"; }while ($i<=5);?>
20. The for Loop The for loop is used when you know in advance how many times the script should run. <?phpfor ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />"; }?>
21. The foreach Loop The foreach loop is used to loop through arrays. <?php$x=array("one","two","three");foreach ($x as $value) { echo $value . "<br />"; }?>
22. Functions A function will be executed by a call to the function. <?phpfunction writeName(){echo "Kai Jim Refsnes";}echo "My name is ";writeName();?>
23. Functions - Adding parameters Parameters are specified after the function name, inside the parentheses. <?phpfunction writeName($fname){echo $fname . " Abraham.<br />";}echo "My name is ";writeName("Kai Jim");echo "My sister's name is ";writeName("Hege");echo "My brother's name is ";writeName("Stale");?>
25. Using the break Statement 1: <?php 2: $counter = -4; 3: for (; $counter <= 10; $counter++) { 4: if ($counter == 0) { 5: break; 6: } else { 7: $temp = 4000/$counter; 8: echo “4000 divided by $counter is... $temp<br>”; 9: } 10: } 11 ?> By The Way Dividing a number by zero does not cause a fatal error in PHP. Instead, PHP generates a warning and execution continues. Did You Know? You can omit any of the expressions from a for statement, but you must remember to retain the separation semicolons.
26. Ouput 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000
27. Using the continue Statement 1: <?php 2: $counter = -4; 3: for (; $counter <= 10; $counter++) { 4: if ($counter == 0) { 5: continue; 6: } 7: $temp = 4000/$counter; 8: echo “4000 divided by $counter is... $temp<br>”; 9: } 10:?> Watch Out! Using the break and continue statements can make code more difficult to read, because they often add layers of complexity to the logic of the loop statements that contain them. Use these statements with care, or comment your code to show other programmers (or yourself) just what you’re trying to achieve with these statements.
28. Output 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000 4000 divided by 1 is... 4000 4000 divided by 2 is... 2000 4000 divided by 3 is... 1333.33333333 4000 divided by 4 is... 1000 4000 divided by 5 is... 800 4000 divided by 6 is... 666.666666667 4000 divided by 7 is... 571.428571429 4000 divided by 8 is... 500 4000 divided by 9 is... 444.444444444
30. Cell Spacing & Cell Padding CELLPADDING and CELLSPACING By default, table cells tend to be squeezed close to each other. To give your table cells a little more breathing room, useCELLPADDING and CELLSPACING. CELLSPACING controls the space between table cells. Although there is no official default, browsers usually use a default of 2. <TABLE BORDER> <TABLE BORDER CELLSPACING=2> <TABLE BORDER CELLSPACING=10> peachescherrieswalnutsalmondspeachescherrieswalnutsalmondspeachescherrieswalnutsalmondsCELLPADDING sets the amount of space between the contents of the cell and the cell wall. The default is 1. CELLPADDING is usually more effective thanCELLSPACING for spreading out the contents of the table. <TABLE BORDER> <TABLE BORDER CELLPADDING=1> <TABLE BORDER CELLPADDING=10> peachescherrieswalnutsalmondspeachescherrieswalnutsalmondspeachescherrieswalnutsalmonds