1 / 10

PHP: Arrays, Strings, and Files

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";

dympna
Download Presentation

PHP: Arrays, Strings, and Files

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. PHP: Arrays, Strings, and Files 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 "<P>"; 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' => "Comstock", 'provost' => "Boyd", 'dean' => "Weikle"); echo "The Dean is ". $bosses['dean'] ."<P>"; The Dean is Weikle

  4. 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

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

  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 BLAHBLAH

  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 <br> 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

More Related