1 / 20

NMD202 Web Scripting

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;

Download Presentation

NMD202 Web Scripting

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NMD202 Web Scripting Week2

  2. Web site http://learnline.cdu.edu.au/units/webscripting

  3. What we will cover today Scope of Variables Predefined variables Exercises Array Functions Exercises Includes Exercises

  4. Scope of variables Function test() { $a=12 $b=13; echo($a-$b) } $a=1; test(); echo($a);

  5. Scope of variables Function test() { global $a; $b=13; echo $a-$b } $a=1; test(); echo($a);

  6. 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)

  7. Predefined variables PHP provides a large number of predefined variables to all scripts (Superglobals): $_SERVER $_GET $_POST $_SESSION $_COOKIE

  8. 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

  9. 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

  10. 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

  11. Predefined variables $_SESSION An associative array containing session variables available to the current script. More on this when we cover Sessions with PHP (week8)

  12. 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)

  13. 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

  14. 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”

  15. 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

  16. Array functions All array functions: http://au.php.net/manual/en/ref.array.php

  17. 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’

  18. 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.

  19. 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

  20. 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.

More Related