html5-img
1 / 69

DIG 3134 – Lecture 4: Arrays and Strings Michael Moshell University of Central Florida

Media Software Design. DIG 3134 – Lecture 4: Arrays and Strings Michael Moshell University of Central Florida. First: Review the Forms Homework. while (answers are not yet enough) { foreach ($class as $student) if ($student is here and awake) call on $student; }.

Download Presentation

DIG 3134 – Lecture 4: Arrays and Strings Michael Moshell University of Central Florida

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Media Software Design DIG 3134 – Lecture 4: Arrays and Strings Michael Moshell University of Central Florida

  2. First: Review the Forms Homework while (answers are not yet enough) { foreach ($class as $student) if ($student is here and awake) call on $student; }

  3. Array: A filing cabinet, or a LIST of variables. $price[1] $price[2] 1 $price[3] 2 $price[4] 3 Value (whoops) Index Variable Name

  4. A For-Loop for ($m=1; $m<=4; $m++) { print “now m is “.$m. “!<br />”; } HAND SIMULATION: value of m: printed output.

  5. A For-Loop For ($m=1; $m<=4; $m++) { print “now m is “.$m. “ !<br />”; } HAND SIMULATION: value of m: printed output.

  6. A For-Loop For ($m=1; $m<=4; $m++) { print “now m is “.$m. “ !<br />”; } HAND SIMULATION: value of m: printed output. 1

  7. A For-Loop For ($m=1; $m<=4; $m++) { print “now m is “.$m. “ !<br />”; } HAND SIMULATION: value of m: printed output. 1 now m is 1!

  8. A For-Loop For ($m=1; $m<=4; $m++) { print “now m is “.$m. “ !<br />”; } HAND SIMULATION: value of m: printed output. 1 now m is 1! 2

  9. A For-Loop For ($m=1; $m<=4; $m++) { print “now m is “.$m. “ !<br />”; } HAND SIMULATION: value of m: printed output. 1 now m is 1! 2 now m is 2!

  10. A For-Loop and an array For ($m=1; $m<=4; $m++) { $list[$m]= 3; } // that will put 3 in each location of $m print $list[2]; //HAND SIMULATE NOW! value of m printed output

  11. A For-Loop and an array: Result after loop finishes For ($m=1; $m<=4; $m++) { $list[$m]= 3; } // that will put 3 in each location of $m print $list[2]; //HAND SIMULATE NOW! value of m printed output 1 2 3 43

  12. A For-Loop and an array For ($m=1; $m<=4; $m++) { $list[$m]= $m; } // that will put $m in each location of $m print $list[2]; // what will this print? value of m printed output

  13. A For-Loop and an array For ($m=1; $m<=4; $m++) { $list[$m]= $m; } // that will put $m in each location of $m print $list[2]; // what will this print? value of m printed output 1 2 3 4 2 13

  14. A For-Loop and an array For ($m=1; $m<=4; $m++) { $list[$m]= $m; } // that will put $m in each location of $m print $list[2]; // what will this print? value of m printed output 1 2 3 4 2 14

  15. A For-Loop and an array For ($m=1; $m<=4; $m++) { $list[$m]= 2*$m; } Print $list[3]; // what will this print?

  16. A while-Loop and an array (we can go faster now) $m=1; while ($m<=7) { $list[$m]= 2*$m; $m=$m+2; } Print $list[5]; // what will this print?

  17. A while-Loop and an array $m=1; While ($m<=7) { $list[$m]= 2*$m; $m=$m+2; } Print $list[4]; // what will this print?

  18. And now … We move to the subject of STRINGS.

  19. The Objective: • Learn how to do all kinds of things • with text in PHP • Why? Numbers are pretty dumb; • Almost all good stuff is words.

  20. String Constants & Variables $n=3; $test= "A bear came over $n mountains."; print $test; // what is printed?

  21. String Constants & Variables $n=3; $test= "A bear came over $n mountains."; print $test; // what is printed? A bear came over 3 mountains.

  22. String Constants & Variables $test= "A bear came over $n mountains."; $n=3; print $test; // what is printed?

  23. String Constants & Variables $test= "A bear came over $n mountains."; $n=3; print $test; // what is printed? A bear came over mountains.

  24. String Constants & Variables $test= "A bear came over $n mountains."; $n=3; print $test; // what is printed? A bear came over mountains. Since nothing had been put into $n, before $test was built, nothing was printed in that spot.

  25. String Constants & Variables $n=3; $test= 'A bear came over $n mountains.'; print $test; // what is printed?

  26. String Constants & Variables $n=3; $test= 'A bear came over $n mountains.'; print $test; // what is printed? A bear came over $n mountains.

  27. String Constants & Variables $n=3; $test= 'A bear came over $n mountains.'; print $test; // what is printed? A bear came over $n mountains. ** because: double quotes are PARSED ** but single quotes are NOT PARSED

  28. String Constants & Variables $n=3; $test= 'A bear came over $n mountains.'; print $test; // what is printed? A bear came over $n mountains. ** because: double quotes are PARSED ** but single quotes are NOT PARSED "Parsed" means – scanned and processed by PHP. "Not parsed:" just print exactly what you see.

  29. String Constants & Variables $n=3; $test= 'A bear came over '.$n.' mountains.'; print $test; // what is printed? A bear came over 3 mountains. ** because: we concatenated three strings (actually 2 strings and 1 integer.) Sometimes "The price is $price[$n]" won't work. But 'The price is '.$price[$n] always works.

  30. So, which should I use? Use single quotes ' ' unless you have a reason to use " " doubles. I often embed variables in string constants because it's quick and simple (if they're not arrays.) In that case, I use double quotes. Like print "The price of coffee is $coffeeprice pesos."; versus print 'The price of coffee is '.$coffeeprice.' pesos';

  31. Concatenation means 'stick things together'. $firstname="Mike"; $secondname="Moshell"; $name=$firstname.$secondname. concatenation operator print $name; MikeMoshell

  32. Concatenation How can I get a space in my name? $firstname="Mike"; $secondname="Moshell"; $name=$firstname.' '.$secondname; include a space print $name; Mike Moshell

  33. Another way to do it $firstname="Mike"; $secondname="Moshell"; $name="$firstname$secondname"; double quotes let PHP parse the string. print $name; Mike Moshell

  34. Anatomy of a String $test= "A bear came over 2 mountains."; Position 0 in the string In PHP a string variable can be ANY LENGTH! You can put an Encyclopedia in a string.

  35. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; I used a monospaced font (Courier) to make it easier to line up two strings and count the characters.

  36. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; what string $piece=substr($test,1,5); Print $piece; // what will this print? 36

  37. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; what string what starting point $piece=substr($test,1,5); Print $piece; // what will this print? 37 37

  38. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; what string what starting point $piece=substr($test,1,5); Print $piece; // what will this print? how far to copy. 38 38 38

  39. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; $piece=substr($test,1,5); Print $piece; // what will this print? he ca Starts in position 1, copies 5 characters 39 39 39

  40. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; $piece=substr($test,4); Print $piece; // what will this print? 40 40 40 40

  41. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; $piece=substr($test,4); Print $piece; // what will this print? cat in the lap. When the 'length' is missing, copy ALL the rest. 41 41 41 41 41

  42. String Position $test= "The cat in the lap."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; What will this print?

  43. String Position $test= "The cat in the lap."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; What will this print? 4 Because the string 'cat' begins at position 4 in $test 43

  44. Cutting up a String $test= "The cat in the lap."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; $before=substr($test,0,$p-1); print "before=$before <br />"; 44

  45. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; $before=substr($test,0,$p-1); print "before=$before <br />"; what starting point

  46. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; $before=substr($test,0,$p-1); print "before=$before <br />"; how far to copy.

  47. Anatomy of a String $test= "The cat in the lap."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; $before=substr($test,0,$p-1); print "before=$before <br />"; p=4 before=The

  48. Challenge #1: $test= "The fat cat sleeps."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; $before=substr($test,0,$p-1); print "before=$before <br />"; WHAT DOES IT PRINT? Hand simulate now.

  49. Anatomy of a String $test= "The fat cat sleeps."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; $before=substr($test,0,$p-1); print "before=$before <br />"; p=8

  50. Anatomy of a String $test= "The fat cat sleeps."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; $before=substr($test,0,$p-1); print "before=$before <br />"; p=8 before=The fat 50

More Related