120 likes | 198 Views
PHP: Arrays, Strings, and Files. CSCI 297 Scripting Languages - Day Three. Arrays - Numeric Indexes. Remember, you don't need to declare variables. PHP automatically indexes new values. you can access specific locations by using [X]. $animal[] = "aardvark"; $animal[] = "bat";
E N D
PHP: Arrays, Strings, and Files CSCI 297 Scripting Languages - Day Three
Arrays - Numeric Indexes • Remember, you don't need to declare variables. • PHP automatically indexes new values. • you can access specific locations by using [X]. $animal[] = "aardvark"; $animal[] = "bat"; $animal[] = "cat"; $animal[1] = "bug"; $animal[4] = "elephant"; print_r($animal); echo "<P>"; Array ( [0] => aardvark [1] => bug [2] => cat [4] => elephant )
Arrays - Associative • Instead of numbers, index is a name. • This is very helpful when processing form data that was passed into the script. • $name = $_POST['yourname']; $bosses = array ('president' => "Comstock", 'provost' => "Boyd", 'dean' => "Weikle"); echo "The Dean is ". $bosses['dean'] ."<P>"; The Dean is Weikle
The "foreach" statement • Foreach is a shorter way to loop through an array. $index = 0; foreach ($animal as $nextanimal) { echo $index ---- $nextanimal <br>"; $index++; } echo "<P>"; foreach ($bosses as $position => $name) echo "$name is the $position <br>"; 0 ---- aardvark1 ---- bug2 ---- cat3 ---- elephant Comstockis the president Boyd is the provost Weikle is the dean
Arrays - misc functions • is_array ($bosses) returns true or false • count($animals) returns the array size • sort ($animals) • $longstring = "Now is the time for all good men…"; • $eachword = explode ($longstring);
Strings - misc functions • there are dozens of functions to manipulate strings • see Chapter 4 of textbook • most of the usual C++ functions (strlen and strcmp) apply in PHP too // string test $answer = str_repeat ("blah ", 3); $answer = strtoupper ($answer); echo $answer; BLAH BLAHBLAH
Strings - misc functions • $name =ucwords ($name);--- capitalize the first letter of each word • ucfirst--- capitalize the first letter • strtolower--- lower case all letters • substr--- copying a substring $string = "Now is the time"; echo substr ($string, 1, 5); // "ow is" echo substr ($string, -4); // "time" • strpos --- search for a substring $location=strpos("Hello World", "H"); // location is 0 $location=strpos("Hello World", "Z"); // location is false
Strings - html processing • nl2br($str) -- output a <br> for each \n in $str • stripslashes -- remove slash characters • trim -- remove leading and trailing spaces, tabs, newlines, etc.
Files - opening • fopen works just like C++ • on daytona, the server writes your files to the /tmp directory • read-write codes: • r = read, pointer at start • w = write, pointer at start • a = write, pointer at end • r+ = read and write $fp= fopen ("/tmp/dannelly.txt", "w") or die ("Could not open for writing"); fwrite ($fp, "Hello World"); fclose ($fp);
Files - misc • To move around use fseek fseek($fp, 0, SEEK_SET); // go to beginning fseek($fp, 0, SEEK_END); // go to end fseek($fp, -20, SEEK_CUR); // move back 20 chars • To dump a whole file into an array $filelines= file ("/tmp/myfile.txt"); • To print a whole file echo file_get_contents("/tmp/dannelly.txt"); more file functions in three weeks