1
IT360: Applied Database Systems PHP Arrays, Files, Functions
Today’s Outline
- Arrays
- Files
- Functions
Todays Outline Arrays Files Functions 1 Arrays <?php - - PDF document
IT360: Applied Database Systems PHP Arrays, Files, Functions Todays Outline Arrays Files Functions 1 Arrays <?php require_once('page.inc.php'); require_once('arrayFunctions.inc.php'); //create the page $myPage = new
<?php require_once('page.inc.php'); require_once('arrayFunctions.inc.php'); //create the page $myPage = new Page('Array examples'); //set the content $myPage->content = '<p>Working with Arrays</p>'; //create an array $array = array('thing1', 'thing2'); $array[2] = 'thing3'; $myPage->content .= '<p>'.displayArray($array) . '</p>'; //create associative array $prices = array('Tires' => 100, 'Oil' => 10); $prices[‘Spark plugs'] = 200; $myPage->content .= '<p>The values in $prices array are <br />’ . displayAssociativeArray($prices) . '</p>'; sort($array); //sort array $myPage->content .= '<p> Sorted array '.displayArray($array) . '</p>'; ksort($prices); //sort associative array by key $myPage->content .= '<p>The values in $prices array sorted by key are <br />' . displayAssociativeArray($prices) . '</p>'; asort($prices); //sort associative array by value $myPage->content .= '<p>The values in $prices array sorted by value are <br />' . displayAssociativeArray($prices) . '</p>'; //display page $myPage->display(); ?>
<?php //return a string to display an array function displayArray($array){ $result = ‘<p>'; for($i = 0; $i < count($array); $i++){ $result .= "Element at index $i is $array[$i] <br />"; } $result .= ‘</p>'; return $result; } //return a string to display an associative array function displayAssociativeArray($array){ $result = ‘<p>'; foreach($array as $key => $value){ $result .= "Element with key $key has value $value <br />"; } $result .= ‘</p>'; return $result; } ?>
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
function save_to_file($text, $fileName = "myFile.txt"){ $fp = @fopen($fileName, 'a'); if (!$fp){ echo "<p>ERROR: Could not open file $fileName. </p>"; return FALSE; } else{ flock($fp, LOCK_EX); fwrite($fp, $text); flock($fp, LOCK_UN); fclose($fp); return TRUE; } }
<?php function start_table($border, $cellspacing=2, $cellpadding=2){ echo “<table border = $border cellspacing = $cellspacing cellpadding = $cellpadding>”; } ?>
<?php function fn(){ $x = 'content'; } fn(); echo 'Value of $x is '. $x; ?>
<?php $x = 'content 1 <br/>'; echo 'Content of $x after initialization is '. $x . '<br />'; function fn(){ echo 'Content of $x at start of function is '. $x. '<br />'; $x = 'content 2 <br/>'; echo 'Content of $x at end of function is '. $x. '<br />'; } fn(); echo 'Value of $x after calling fn() is '. $x. ‘<br />'; ?>