"Boyd", 'dean' => "Weikle"); echo "The Dean is ". $bosses['dean']. " "; The Dean is Weikle"> "Boyd", 'dean' => "Weikle"); echo "The Dean is ". $bosses['dean']. " "; The Dean is Weikle">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three.

Similar presentations


Presentation on theme: "PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three."— Presentation transcript:

1 PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three

2 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 " "; Array ( [0] => aardvark [1] => bug [2] => cat [4] => elephant )

3 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' => "Mahony", 'provost' => "Boyd", 'dean' => "Weikle"); echo "The Dean is ". $bosses['dean']. " "; The Dean is Weikle

4 The "foreach" statement use "foreach" to loop through an array. $index = 0; foreach ($animal as $nextanimal) { echo $index ---- $nextanimal "; $index++; } echo " "; foreach ($bosses as $position => $name) echo "$name is the $position "; 0 ---- aardvark 1 ---- bug 2 ---- cat 3 ---- elephant Mahony is the president Boyd is the provost Weikle is the dean WRONG

5 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); unset ($animals[0]) removes aardvark in_array ( 5, $values) is 5 in the array $values?

6 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 BLAH BLAH

7 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

8 Strings - html processing nl2br($str) -- output a for each \n in $str stripslashes -- remove slash characters trim -- remove leading and trailing spaces, tabs, newlines, etc.

9 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);

10 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


Download ppt "PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three."

Similar presentations


Ads by Google