Php 2

1,166 views
1,082 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,166
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php 2

  1. 1. Conditional Statements<br />In PHP we have the following conditional statements:<br /><ul><li>if statement - use this statement to execute some code only if a specified condition is true
  2. 2. if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
  3. 3. if...elseif....else statement - use this statement to select one of several blocks of code to be executed
  4. 4. switch statement - use this statement to select one of many blocks of code to be executed</li></li></ul><li>The if Statement<br /><?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!";?><br />
  5. 5. The if...else Statement<br /><?php$d=date("D");if ($d=="Fri")  echo "Have a nice weekend!";else  echo "Have a nice day!";?><br />
  6. 6. The if...elseif....else Statement<br /><?php<br />$d=date("D");<br />if ($d=="Fri")  echo "Have a nice weekend!";<br />elseif ($d=="Sun")  echo "Have a nice Sunday!";<br />else  echo "Have a nice day!";<br />?><br />
  7. 7. Switch Statement<br /><?phpswitch ($x){case 1:  echo "Number 1";  break;case 2:  echo "Number 2";  break;case 3:  echo "Number 3";  break;default:  echo "No number between 1 and 3";}?><br />
  8. 8. What is an Array?<br />An array is a special variable, which can store multiple values in one single variable.<br />There are three kind of arrays:<br /><ul><li>Numeric array - An array with a numeric index
  9. 9. Associative array - An array where each ID key is associated with a value
  10. 10. Multidimensional array - An array containing one or more arrays</li></li></ul><li>Numeric Arrays<br />A numeric array stores each array element with a numeric index.<br />$cars=array("Saab","Volvo","BMW","Toyota"); <br /><?php$cars[0]="Saab";$cars[1]="Volvo";$cars[2]="BMW";$cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";?><br />
  11. 11. Associative Arrays<br />An associative array, each ID key is associated with a value.<br />$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); <br />$ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34"; <br />
  12. 12. <?php$ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34";echo "Peter is " . $ages['Peter'] . " years old.";<br />?> <br />
  13. 13. Multidimensional Arrays<br />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.<br />$families = array  (  "Griffin"=>array  (  "Peter",  "Lois",  "Megan"  ),  "Quagmire"=>array  (  "Glenn"  ),  "Brown"=>array  (  "Cleveland",  "Loretta",  "Junior"  )  ); <br />
  14. 14. Array([Griffin] => Array  (  [0] => Peter  [1] => Lois  [2] => Megan  )[Quagmire] => Array  (  [0] => Glenn  )[Brown] => Array  (  [0] => Cleveland  [1] => Loretta  [2] => Junior  )) <br />
  15. 15. Loops<br />In PHP, we have the following looping statements:<br /><ul><li>while - loops through a block of code while a specified condition is true
  16. 16. do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
  17. 17. for - loops through a block of code a specified number of times
  18. 18. foreach- loops through a block of code for each element in an array</li></li></ul><li>The while Loop<br />The while loop executes a block of code while a condition is true.<br /><?php$i=1;while($i<=5)  {  echo "The number is " . $i . "<br />";  $i++;  }?><br />
  19. 19. The do...while Statement<br />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.<br /><?php$i=1;do  {  $i++;  echo "The number is " . $i . "<br />";  }while ($i<=5);?><br />
  20. 20. The for Loop<br />The for loop is used when you know in advance how many times the script should run.<br /><?phpfor ($i=1; $i<=5; $i++)  {  echo "The number is " . $i . "<br />";  }?><br />
  21. 21. The foreach Loop<br />The foreach loop is used to loop through arrays.<br /><?php$x=array("one","two","three");foreach ($x as $value)  {  echo $value . "<br />";  }?><br />
  22. 22. Functions<br />A function will be executed by a call to the function.<br /><?phpfunction writeName(){echo "Kai Jim Refsnes";}echo "My name is ";writeName();?><br />
  23. 23. Functions - Adding parameters<br />Parameters are specified after the function name, inside the parentheses.<br /><?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");?><br />
  24. 24. Functions - Return values<br /><?phpfunction add($x,$y){$total=$x+$y;return $total;}<br />echo "1 + 16 = " . add(1,16);<br />?><br />
  25. 25. Using the break Statement<br />1: <?php<br />2: $counter = -4;<br />3: for (; $counter <= 10; $counter++) {<br />4: if ($counter == 0) {<br />5: break;<br />6: } else {<br />7: $temp = 4000/$counter;<br />8: echo “4000 divided by $counter is... $temp<br>”;<br />9: }<br />10: }<br />11 ?><br />By The Way<br />Dividing a number by zero does not cause a fatal error in PHP. Instead, PHP generates a warning and execution continues.<br />Did You Know?<br />You can omit any of the expressions from a for statement, but you must remember to retain the separation semicolons.<br />
  26. 26. Ouput<br />4000 divided by -4 is... -1000<br />4000 divided by -3 is... -1333.33333333<br />4000 divided by -2 is... -2000<br />4000 divided by -1 is... -4000<br />
  27. 27. Using the continue Statement<br />1: <?php<br />2: $counter = -4;<br />3: for (; $counter <= 10; $counter++) {<br />4: if ($counter == 0) {<br />5: continue;<br />6: }<br />7: $temp = 4000/$counter;<br />8: echo “4000 divided by $counter is... $temp<br>”;<br />9: }<br />10:?><br />Watch Out!<br />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.<br />
  28. 28. Output<br />4000 divided by -4 is... -1000<br />4000 divided by -3 is... -1333.33333333<br />4000 divided by -2 is... -2000<br />4000 divided by -1 is... -4000<br />4000 divided by 1 is... 4000<br />4000 divided by 2 is... 2000<br />4000 divided by 3 is... 1333.33333333<br />4000 divided by 4 is... 1000<br />4000 divided by 5 is... 800<br />4000 divided by 6 is... 666.666666667<br />4000 divided by 7 is... 571.428571429<br />4000 divided by 8 is... 500<br />4000 divided by 9 is... 444.444444444<br />
  29. 29. Nested Looping<br />1: <?php<br />2: echo “<table border=”1” cellpadding=”4” cellspacing=”4”> ”;<br />3: for ($y=1; $y<=12; $y++) {<br />4: echo “<tr> ”;<br />5: for ($x=1; $x<=12; $x++) {<br />6: echo “<td>”;<br />7: echo ($x * $y);<br />8: echo “</td> ”;<br />9:}<br />10:echo “</tr> ”;<br />11: }<br />12: echo “</table>”;<br />13: ?><br />
  30. 30. Cell Spacing & Cell Padding<br />CELLPADDING and CELLSPACING<br />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.<br />CELLSPACING controls the space between table cells. Although there is no official default, browsers usually use a default of 2. <br /><TABLE BORDER> <br /><TABLE BORDER CELLSPACING=2> <br /><TABLE BORDER CELLSPACING=10> <br />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. <br /><TABLE BORDER> <br /><TABLE BORDER CELLPADDING=1> <br /><TABLE BORDER CELLPADDING=10> <br />peachescherrieswalnutsalmondspeachescherrieswalnutsalmondspeachescherrieswalnutsalmonds<br />

×