200 likes | 298 Views
NMD202 Web Scripting. Week2. Web site. http://learnline.cdu.edu.au/units/webscripting. What we will cover today. Scope of Variables Predefined variables Exercises Array Functions Exercises Includes Exercises. Scope of variables. Function test() { $a=12 $b=13;
E N D
NMD202 Web Scripting Week2
Web site http://learnline.cdu.edu.au/units/webscripting
What we will cover today Scope of Variables Predefined variables Exercises Array Functions Exercises Includes Exercises
Scope of variables Function test() { $a=12 $b=13; echo($a-$b) } $a=1; test(); echo($a);
Scope of variables Function test() { global $a; $b=13; echo $a-$b } $a=1; test(); echo($a);
Scope of variables Best Practices: Use global with care and only when necessary Use different variable names whenever possible Use human readable names whenever possible (be sensible)
Predefined variables PHP provides a large number of predefined variables to all scripts (Superglobals): $_SERVER $_GET $_POST $_SESSION $_COOKIE
Predefined variables $_SERVER $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server Some information may or may not be there depending on the server and if PHP is running on the command line interface. http://au2.php.net/manual/en/reserved.variables.server.php
Predefined variables $_GET An associative array of variables passed to the current script via the HTTP GET method. http://example.com/?name=Luis echo $_GET["name"] -> output Luis
Predefined variables $_POST An associative array of variables passed to the current script via the HTTP POST method. <input type=“text” name=“firstName” value=“Luis” /> echo $_POST[" firstName"] -> output Luis
Predefined variables $_SESSION An associative array containing session variables available to the current script. More on this when we cover Sessions with PHP (week8)
Predefined variables $_COOKIES An associative array of variables passed to the current script via HTTP Cookies. More on this when we cover Sessions with PHP (week8)
Exercises Setup Development Environment Write a function that returns an associative array containing: • User IP Address • Date and Time Request has been made, properly formatted • Output the array as an unordered list • Tip: Use date function to convert timestamp
Array functions implode - Join array elements with a string implode(array(‘hello’,’world’),’ ‘); // returns “hello world”; array_push, array_pop - adds/ subtracts element array array_slice — extract a slice of the array $input = array("a", "b", "c", "d", "e"); $output = array_slice($input, 2,1); //returns “c”
Array functions array_reverse - Return an array with elements in reverse order array_search — Searches the array for a given value and returns the corresponding key if successful count - count elements in an array in_array — Checks if a value exists in an array array_key_exists — Checks if the given key or index exists in the array
Array functions All array functions: http://au.php.net/manual/en/ref.array.php
Multi dimensional Arrays each array element can contain another array as a value, which in turn can hold other arrays as well. In such a way you can create two-dimensional or three-dimensional arrays. $products = array( array(“name”=>”flower”,”price”=>”12”), array(“name”=>”pot”,”price”=>”50”), ); echo $products[0][‘name’]; //return ‘flower’
Exercises Store a table with students and information (name, ID, password) in a multi-dimensional array "stud". Output the stud table in form of a table using a loop. Using a query string (Get method) filter the table to present only the student passed on the URL ie: table.php?name=luis, if name is not on array then output an error message.
Includes The include($filename) statement includes and evaluates the specified file. require($filename), does the same thing except it halt execution if $filename is not found include_once($filename), require_once($filename), file is included only once if called several times
Exercises Redo last exercise but split your file into logical sections (templating), ie:Include the head of your document, the body, the footer, etc. Place the stud array in an external file and include it in the main script.