690 likes | 784 Views
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; }.
E N D
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; }
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
A For-Loop for ($m=1; $m<=4; $m++) { print “now m is “.$m. “!<br />”; } HAND SIMULATION: value of m: printed output.
A For-Loop For ($m=1; $m<=4; $m++) { print “now m is “.$m. “ !<br />”; } HAND SIMULATION: value of m: printed output.
A For-Loop For ($m=1; $m<=4; $m++) { print “now m is “.$m. “ !<br />”; } HAND SIMULATION: value of m: printed output. 1
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!
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
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!
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
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
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
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
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
A For-Loop and an array For ($m=1; $m<=4; $m++) { $list[$m]= 2*$m; } Print $list[3]; // what will this print?
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?
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?
And now … We move to the subject of STRINGS.
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.
String Constants & Variables $n=3; $test= "A bear came over $n mountains."; print $test; // what is printed?
String Constants & Variables $n=3; $test= "A bear came over $n mountains."; print $test; // what is printed? A bear came over 3 mountains.
String Constants & Variables $test= "A bear came over $n mountains."; $n=3; print $test; // what is printed?
String Constants & Variables $test= "A bear came over $n mountains."; $n=3; print $test; // what is printed? A bear came over mountains.
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.
String Constants & Variables $n=3; $test= 'A bear came over $n mountains.'; print $test; // what is printed?
String Constants & Variables $n=3; $test= 'A bear came over $n mountains.'; print $test; // what is printed? A bear came over $n mountains.
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
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.
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.
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';
Concatenation means 'stick things together'. $firstname="Mike"; $secondname="Moshell"; $name=$firstname.$secondname. concatenation operator print $name; MikeMoshell
Concatenation How can I get a space in my name? $firstname="Mike"; $secondname="Moshell"; $name=$firstname.' '.$secondname; include a space print $name; Mike Moshell
Another way to do it $firstname="Mike"; $secondname="Moshell"; $name="$firstname$secondname"; double quotes let PHP parse the string. print $name; Mike Moshell
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.
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.
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
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
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
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
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
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
String Position $test= "The cat in the lap."; $nums= "0123456789012345678"; $p=strpos($test,"cat"); print "p=$p <br />"; What will this print?
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
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
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
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.
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
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.
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
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