Quick viewing(Text Mode)

PHP Arrays, Files, Functions Lecture

IT360: Applied Systems

PHP Arrays, Files, Functions

Today’s Outline

. Arrays . Files . Functions

1 Arrays

//create the page $myPage = new Page('Array examples');

// the content $myPage->content = '

Working with Arrays

';

//create an array $array = array('thing1', 'thing2'); $array[2] = 'thing3';

$myPage->content .= '

'.displayArray($array) . '

';

//create associative array $prices = array('Tires' => 100, 'Oil' => 10); $prices[‘Spark plugs'] = 200;

$myPage->content .= '

The values in $prices array are
’ . displayAssociativeArray($prices) . '

'; sort($array); //sort array $myPage->content .= '

Sorted array '.displayArray($array) . '

';

ksort($prices); //sort associative array by key $myPage->content .= '

The values in $prices array sorted by key are
' . displayAssociativeArray($prices) . '

';

asort($prices); //sort associative array by value $myPage->content .= '

The values in $prices array sorted by value are
' . displayAssociativeArray($prices) . '

'; //display page $myPage->display(); ?>

array_functions.inc.php

//return a string to display an array function displayArray($array){ $result = ‘

'; for($i = 0; $i < count($array); $i++){ $result .= "Element at index $i is $array[$i]
"; } $result .= ‘

';

return $result;

}

//return a string to display an associative array function displayAssociativeArray($array){ $result = ‘

'; foreach($array as $key => $value){ $result .= "Element with key $key has value $value
"; } $result .= ‘

'; return $result;

}

?>

2 Class Exercise

. Create an array containing the following values: (1900, 2000, 2004, 2005 ). . Use the array in a “foreach” loop to test if the value is a leap year. . If it is a leap year print “XXXX is a leap year” . Else print “XXXX is not a leap year” . A year is a leap year if it is divisible by 4. If the year is also divisible by 100 then it is NOT a leap year unless it is also divisible by 400. Thus, 1900 is not a leap year (divisible by 100 but not 400) while 2000 is a leap year (divisible by 400).

3 Outline

. Arrays . More About Files . More About Functions

fopen() modes

r Read only. Starts at beginning of file r+ Read/Write. Starts at beginning of file w Write only. Opens and clears contents of file; or creates new file if it doesn't exist w+ Read/Write. Opens and clears contents of file; or creates new file if it doesn't exist a Write only. Opens and writes to end of file or creates new file if it doesn't exist a+ Read/Write. Preserves file content by writing to end of file x Write only. Creates new file. Returns FALSE and error if file already exists x+ Read/Write. Creates new file. Returns FALSE and error if file already exists

4 More Files: Read Line from File

. string = fgets(filePointer, [maxLength])

. string = fgetss(filePointer, [maxLength] [, allowableTags])

. array = fgetcsv(filePointer, [maxLength] [, string delimiter])

Read Whole File

. int readfile(fileName)

. int fpassthru(filePointer)

. array file(fileName)

. string file_get_contents(fileName)

5 Other Reads

. char fgetc(filePointer)

. string fread(filePointer, nbBytes)

Useful File Functions

. bool file_exists(fileName) . int filesize(fileName) . bool unlink(fileName)

6 File Locking function save_to_file($text, $fileName = "myFile.txt"){

$fp = @fopen($fileName, 'a');

if (!$fp){ echo "

ERROR: Could not open file $fileName.

"; return FALSE; } else{

flock($fp, LOCK_EX);

fwrite($fp, $text);

flock($fp, LOCK_UN);

fclose($fp); return TRUE; } }

Class Exercise

. Create PHP script to: . Open/create a file, without overwriting it . Write the numbers 1 to 20 to file, separated by space . Close the file

7 Outline

. Arrays . More About Files . More About Functions

Function Parameters

”; } ?> . start_table(2,3,4) . start_table(1) . start_table(2,3)

8 Variables Scope

. Variables used in functions are visible from first use line to end of function – local variables . Variables used outside functions are visible from first use line to end of file, but not inside functions – global variables . Superglobal variables ($_POST, $_SERVER, …) are visible everywhere . Keyword global makes local variables global – not recommended

Variables Scope Example

9 Variables Scope Example 2 '; echo 'Content of $x after initialization is '. $x . '
'; function fn(){ echo 'Content of $x at start of function is '. $x. '
'; $x = 'content 2
'; echo 'Content of $x at end of function is '. $x. '
'; } fn(); echo 'Value of $x after calling fn() is '. $x. ‘
'; ?>

Summary

. Arrays . Numerically indexed . Associative . Sort . Files . Functions to work with files . File locking . Functions . Optional parameters . Variable scope

10