php arrays strings and files
Download
Skip this Video
Download Presentation
PHP: Arrays, Strings, and Files

Loading in 2 Seconds...

play fullscreen
1 / 10

PHP: Arrays, Strings, and Files - PowerPoint PPT Presentation


  • 52 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' PHP: Arrays, Strings, and Files' - dympna


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

ad