Php arrays strings and files
This presentation is the property of its rightful owner.
Sponsored Links
1 / 10

PHP: Arrays, Strings, and Files PowerPoint PPT Presentation


  • 32 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Php arrays strings and files

PHP: Arrays, Strings, and Files

CSCI 297 Scripting Languages - Day Three


Arrays numeric indexes

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

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

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

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

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 functions1

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

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

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

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


  • Login