eg-259:lecture17
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
eg-259:lecture17 [2007/11/27 09:27] – eechris | eg-259:lecture17 [2013/03/08 18:04] (current) – [Introduction to PHP (Part 2)] eechris | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | |||
+ | ~~SLIDESHOW~~ | ||
+ | ====== Introduction to PHP (Part 2) ====== | ||
+ | |||
+ | **Supplementary Material** | ||
+ | |||
+ | Provided for Reference. This material is no longer taught on this module. | ||
+ | |||
+ | **Lecturer**: | ||
+ | |||
+ | More on the language features of PHP | ||
+ | |||
+ | |||
+ | |||
+ | ===== Introduction to PHP (Part 2) ===== | ||
+ | |||
+ | We continue our review of the Basics of PHP with a discussion of //arrays//, // | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Based on Chapter 12 of Robert W. Sebasta, // | ||
+ | |||
+ | ===== Contents of this Lecture ===== | ||
+ | |||
+ | //Further features of the PHP language with examples// | ||
+ | |||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | |||
+ | The [[eg-259: | ||
+ | |||
+ | |||
+ | ===== Learning Outcomes ==== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - In what two ways can arrays in PHP be created? | ||
+ | - What keys are used when an array is created but no keys are specified? | ||
+ | - Must all of the values of an array be of the same type? | ||
+ | - Must all of the keys of an array be of the same type? | ||
+ | - What exactly do the '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What exactly does the '' | ||
+ | - Explain the actions of the '' | ||
+ | - Describe the actions of the '' | ||
+ | - What are the syntax and semantics of the two forms of the '' | ||
+ | - Describe the result of using the '' | ||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What is the difference between the '' | ||
+ | - What happens if a script defines the same function more than once? | ||
+ | - Are function names case sensitive? | ||
+ | - What value is returned by a function if its execution does not end by executing a '' | ||
+ | |||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What are the two ways you can specify that a parameter is to be passed by reference? | ||
+ | - How can a variable used outside a function be accessed by the function? | ||
+ | - How can you define a variable in a function so that its lifetime extends beyond the time the function is in its first execution? | ||
+ | |||
+ | |||
+ | ===== Arrays ===== | ||
+ | |||
+ | * Not like the arrays of any other programming language | ||
+ | * A PHP array is a // | ||
+ | * A PHP array is really a mapping of keys to values, where the keys can be numbers (to get a // | ||
+ | |||
+ | |||
+ | ===== Array creation ===== | ||
+ | |||
+ | * Use the '' | ||
+ | * The keys are non-negative integer literals or string literals | ||
+ | * The values can be anything | ||
+ | * // | ||
+ | <code php> | ||
+ | $list = array(0 => " | ||
+ | </ | ||
+ | * This is a " | ||
+ | |||
+ | |||
+ | ===== Array creation (continued) ===== | ||
+ | |||
+ | * If a key is omitted and there have been integer keys, the default key will be the largest current key + 1 | ||
+ | * If a key is omitted and there have been no integer keys, 0 is the default key | ||
+ | * If a key appears that has already appeared, the new value will overwrite the old one | ||
+ | |||
+ | |||
+ | |||
+ | ===== Array elements ===== | ||
+ | |||
+ | * Arrays can have mixed kinds of elements | ||
+ | * //Some examples//: | ||
+ | <code php> | ||
+ | $list = array(" | ||
+ | " | ||
+ | |||
+ | $list = array(1, 3, 5, 7, 9); | ||
+ | |||
+ | $list = array(5, 3 => 7, 5 => 10, " | ||
+ | |||
+ | $colours = array(' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Accessing array elements ===== | ||
+ | |||
+ | * //Use brackets//: | ||
+ | <code php> | ||
+ | $list[4] = 7; | ||
+ | $list[" | ||
+ | $list[] = 17; | ||
+ | </ | ||
+ | * If an element with the specified key does not exist, it is created | ||
+ | * If the array does not exist, the array is created | ||
+ | |||
+ | |||
+ | ===== Accessing array elements ===== | ||
+ | |||
+ | * The keys or values can be extracted from an array: | ||
+ | <code php> | ||
+ | $highs = array(" | ||
+ | " | ||
+ | " | ||
+ | $days = array_keys($highs); | ||
+ | $temps = array_values($highs); | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Dealing with Arrays ===== | ||
+ | |||
+ | * An array can be deleted with '' | ||
+ | <code php> | ||
+ | unset($list); | ||
+ | unset($list[4]); | ||
+ | </ | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | ===== Dealing with Arrays (continued) ===== | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Internal structure of arrays | ||
+ | |||
+ | {{eg-259: | ||
+ | |||
+ | |||
+ | |||
+ | ===== Sequential access to array elements ===== | ||
+ | |||
+ | * Use '' | ||
+ | <code php> | ||
+ | $colours = array(" | ||
+ | $colour = current($colours); | ||
+ | print(" | ||
+ | while ( $colour = next($colours) ) { | ||
+ | print (" | ||
+ | } | ||
+ | </ | ||
+ | * This does not always work -- for example, if the value in the array happens to be '' | ||
+ | |||
+ | |||
+ | |||
+ | ===== Sequential access to array elements (continued) ===== | ||
+ | |||
+ | * // | ||
+ | <code php> | ||
+ | while ( $element = each($colours) ) { | ||
+ | print (" | ||
+ | } | ||
+ | </ | ||
+ | * The '' | ||
+ | |||
+ | ===== Sequential access to array elements (continued) ===== | ||
+ | |||
+ | * To implement stacks with an array use '' | ||
+ | |||
+ | |||
+ | ===== Sequential access to array elements (continued) ===== | ||
+ | |||
+ | * To visit every element of an array use '' | ||
+ | <code php> | ||
+ | foreach ($colours as $colour) { | ||
+ | print "Is $colour your favourite colour?< | ||
+ | } | ||
+ | </ | ||
+ | * // | ||
+ | | ||
+ | Is red your favourite colour? | ||
+ | Is blue your favourite colour? | ||
+ | Is green your favourite colour? | ||
+ | Is yellow your favourite colour? | ||
+ | |||
+ | |||
+ | |||
+ | ===== Sequential access to array elements (continued) ===== | ||
+ | |||
+ | * '' | ||
+ | <code php> | ||
+ | foreach ($colours as $key => $colour) { ... } | ||
+ | </ | ||
+ | * Inside the compound statement, both '' | ||
+ | * // | ||
+ | <code php> | ||
+ | $ages = array(" | ||
+ | foreach ($ages as $name => $age) { | ||
+ | print(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Sorting Arrays ===== | ||
+ | |||
+ | * '' | ||
+ | *e.g., '' | ||
+ | * The '' | ||
+ | * Works for both strings and numbers, even mixed strings and numbers: | ||
+ | <code php> | ||
+ | $list = (' | ||
+ | sort($list); | ||
+ | // Produces (' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Sorting Arrays (continued) ===== | ||
+ | |||
+ | * In PHP 4+, the sort function can take a second parameter, which specifies a particular kind of sort: | ||
+ | <code php> | ||
+ | sort($list, SORT_NUMERIC); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== More sorting functions ===== | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | <code php> | ||
+ | $list(" | ||
+ | ksort($list); | ||
+ | // $list is now (" | ||
+ | </ | ||
+ | * '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Example of Array Sorting ===== | ||
+ | |||
+ | * An example to illustrate several of the sorting functions: [[/ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | * //Code//: | ||
+ | <code php> | ||
+ | << | ||
+ | |||
+ | <!-- sorting.php - An example to illustrate several of the | ||
+ | | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | | ||
+ | </ | ||
+ | < | ||
+ | <?php | ||
+ | | ||
+ | " | ||
+ | " | ||
+ | ?> | ||
+ | <h4> Original Array </h4> | ||
+ | <?php | ||
+ | foreach ($original as $key => $value) { | ||
+ | | ||
+ | } | ||
+ | ?> | ||
+ | <h4> Array sorted with sort </h4> | ||
+ | <?php | ||
+ | $new = $original; | ||
+ | sort($new); | ||
+ | | ||
+ | | ||
+ | } | ||
+ | ?> | ||
+ | <h4> Array sorted with asort </h4> | ||
+ | <?php | ||
+ | $new = $original; | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | ?> | ||
+ | <h4> Array sorted with ksort </h4> | ||
+ | <?php | ||
+ | $new = $original; | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | * //Result//: | ||
+ | |||
+ | | ||
+ | |||
+ | |||
+ | |||
+ | ===== User-Defined Functions ===== | ||
+ | |||
+ | * //Syntactic form//: | ||
+ | <code php> | ||
+ | function function_name( [formal_parameters] ) { | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== General Characteristics of Functions ===== | ||
+ | |||
+ | * Functions need not be defined before they are called (in PHP 3, they must) | ||
+ | * Function overloading is not supported((PPHP 5 supports classes, so it may be possible to use overloading for polymorphism)) | ||
+ | * If you try to redefine a function, it is an error | ||
+ | * Functions can have a variable number of parameters | ||
+ | * Default parameter values are supported | ||
+ | * Function definitions can be nested | ||
+ | * Function names are not case sensitive | ||
+ | * The '' | ||
+ | |||
+ | |||
+ | ===== Function Parameters ===== | ||
+ | |||
+ | * If the caller sends too many actual parameters, the subprogram ignores the extra ones | ||
+ | * If the caller does not send enough parameters, the unmatched formal parameters are '' | ||
+ | * The default parameter passing method is //pass by value// (one-way communication) | ||
+ | |||
+ | ===== Function Parameters (continued) ===== | ||
+ | |||
+ | * To specify // | ||
+ | <code php> | ||
+ | function addOne(& | ||
+ | | ||
+ | } | ||
+ | |||
+ | $it = 16; | ||
+ | addOne($it); | ||
+ | </ | ||
+ | |||
+ | ===== Function Parameters (continued) ===== | ||
+ | |||
+ | * If the function does not specify its parameter to be pass by reference, you can prepend an ampersand to the actual parameter and still get pass-by-reference semantics: | ||
+ | <code php> | ||
+ | function subOne($param) { | ||
+ | $param--; | ||
+ | } | ||
+ | | ||
+ | $it = 16; | ||
+ | subOne(& | ||
+ | </ | ||
+ | |||
+ | ===== Return Values ===== | ||
+ | |||
+ | * Any type may be returned, including objects and arrays, using the '' | ||
+ | * If a function returns a reference, the name of the function must have a prepended ampersand: | ||
+ | <code php> | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ===== The Scope of Variables ===== | ||
+ | |||
+ | * An undeclared variable in a function has the scope of the function | ||
+ | * To access a nonlocal variable, it must be declared to be //global//, as in: | ||
+ | <code php> | ||
+ | | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== The Lifetime of Variables ===== | ||
+ | |||
+ | * Normally, the lifetime of a variable in a function is from its first appearance to the end of the function' | ||
+ | <code php> | ||
+ | static $sum = 0; # $sum is static | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Pattern Matching ===== | ||
+ | |||
+ | * PHP has two kinds: | ||
+ | * POSIX | ||
+ | * Perl-compatible ('' | ||
+ | <code php> | ||
+ | preg_match(regex, | ||
+ | </ | ||
+ | * The optional '' | ||
+ | |||
+ | |||
+ | ===== Example ===== | ||
+ | |||
+ | The [[/ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | * //Code//: | ||
+ | <code php> | ||
+ | < | ||
+ | < | ||
+ | |||
+ | Uses a function to split a given string of text into | ||
+ | its constituent words. It also determines the frequency of | ||
+ | occurrence of each word. The words are separated by | ||
+ | whitespace or punctuation, | ||
+ | The punctuation can be a period, a comma, a semicolon, a | ||
+ | colon, an exclamation point, or a question mark. | ||
+ | The main driver program calls the function and displays | ||
+ | the results. | ||
+ | --> | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <?php | ||
+ | |||
+ | // Function splitter | ||
+ | // Parameter: a string of text containing words and punctuation | ||
+ | // Returns: an array in which the unique words of the string are | ||
+ | // the keys and their frequencies are the values. | ||
+ | |||
+ | function splitter($str) { | ||
+ | |||
+ | // Create the empty word frequency array | ||
+ | |||
+ | $freq = array(); | ||
+ | |||
+ | // Split the parameter string into words | ||
+ | |||
+ | $words = preg_split("/ | ||
+ | |||
+ | // Loop to count the words (either increment or initialize to 1) | ||
+ | |||
+ | foreach ($words as $word) { | ||
+ | $keys = array_keys($freq); | ||
+ | if (in_array($word, | ||
+ | $freq[$word]++; | ||
+ | else | ||
+ | $freq[$word] = 1; | ||
+ | } | ||
+ | |||
+ | return $freq; | ||
+ | }#** End of splitter | ||
+ | |||
+ | // Main test driver | ||
+ | |||
+ | $str = " | ||
+ | or maybe you like oranges better than apples"; | ||
+ | |||
+ | // Call splitter | ||
+ | |||
+ | $tbl = splitter($str); | ||
+ | |||
+ | // Display the words and their frequencies | ||
+ | |||
+ | print "< | ||
+ | $sorted_keys = array_keys($tbl); | ||
+ | sort($sorted_keys); | ||
+ | foreach ($sorted_keys as $word) { | ||
+ | print "$word $tbl[$word] <br />"; | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | * Output of the example script | ||
+ | |||
+ | Word Frequency | ||
+ | | ||
+ | apples 3 | ||
+ | are 1 | ||
+ | better 1 | ||
+ | don't 1 | ||
+ | for 1 | ||
+ | good 1 | ||
+ | like 2 | ||
+ | maybe 1 | ||
+ | or 2 | ||
+ | oranges 1 | ||
+ | than 1 | ||
+ | you 3 | ||
+ | |||
+ | ===== Summary of this Lecture ===== | ||
+ | |||
+ | //Further features of the PHP language with examples// | ||
+ | |||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | |||
+ | The [[eg-259: | ||
+ | |||
+ | ===== Learning Outcomes ==== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - In what two ways can arrays in PHP be created? | ||
+ | - What keys are used when an array is created but no keys are specified? | ||
+ | - Must all of the values of an array be of the same type? | ||
+ | - Must all of the keys of an array be of the same type? | ||
+ | - What exactly do the '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What exactly does the '' | ||
+ | - Explain the actions of the '' | ||
+ | - Describe the actions of the '' | ||
+ | - What are the syntax and semantics of the two forms of the '' | ||
+ | - Describe the result of using the '' | ||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What is the difference between the '' | ||
+ | - What happens if a script defines the same function more than once? | ||
+ | - Are function names case sensitive? | ||
+ | - What value is returned by a function if its execution does not end by executing a '' | ||
+ | |||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What are the two ways you can specify that a parameter is to be passed by reference? | ||
+ | - How can a variable used outside a function be accessed by the function? | ||
+ | - How can you define a variable in a function so that its lifetime extends beyond the time the function is in its first execution? | ||
+ | |||
+ | |||
+ | ===== Homework Exercises ===== | ||
+ | |||
+ | Write the following PHP functions and code to test them | ||
+ | |||
+ | - // | ||
+ | - // | ||
+ | - // | ||
+ | |||
+ | |||
+ | ===== Homework Exercises (continued) ===== | ||
+ | |||
+ | - Modify the sample script '' | ||
+ | |||
+ | These and other PHP exercises are also available on the [[eg-259: | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== What's Next? ===== | ||
+ | |||
+ | **PHP for Web Applications** | ||
+ | |||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | |||
+ | |||
+ | |||
+ | [[eg-259: |