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 [2012/03/13 20:06] – [Introduction to PHP (Part 2)] eechris | eg-259:lecture17 [2013/03/08 18:04] (current) – [Introduction to PHP (Part 2)] eechris | ||
---|---|---|---|
Line 4: | Line 4: | ||
====== Introduction to PHP (Part 2) ====== | ====== Introduction to PHP (Part 2) ====== | ||
- | **Contact Hour 20**: To be discussed | + | **Supplementary Material** |
+ | |||
+ | Provided for Reference. This material is no longer taught | ||
**Lecturer**: | **Lecturer**: | ||
More on the language features of PHP | More on the language features of PHP | ||
- | |||
Line 280: | Line 281: | ||
* //Code//: | * //Code//: | ||
<code php> | <code php> | ||
- | <?php echo("<? | + | << |
- | < | + | |
- | " | + | |
<!-- sorting.php - An example to illustrate several of the | <!-- sorting.php - An example to illustrate several of the | ||
| | ||
- | < | + | <html lang=" |
- | < | + | < |
+ | <meta charset=" | ||
+ | | ||
+ | | ||
</ | </ | ||
< | < | ||
Line 430: | Line 432: | ||
* //Code//: | * //Code//: | ||
<code php> | <code php> | ||
- | <?php echo("<? | + | < |
- | < | + | |
- | " | + | |
< | < | ||
- | | + | Uses a function to split a given string of text into |
- | its constituent words. It also determines the frequency of | + | its constituent words. It also determines the frequency of |
- | occurrence of each word. The words are separated by | + | occurrence of each word. The words are separated by |
- | whitespace or punctuation, | + | whitespace or punctuation, |
- | The punctuation can be a period, a comma, a semicolon, a | + | The punctuation can be a period, a comma, a semicolon, a |
- | colon, an exclamation point, or a question mark. | + | colon, an exclamation point, or a question mark. |
- | The main driver program calls the function and displays | + | The main driver program calls the function and displays |
- | the results. | + | the results. |
- | --> | + | --> |
- | < | + | <html lang=" |
- | < | + | < |
- | </ | + | <meta charset=" |
- | < | + | |
- | <?php | + | </ |
+ | < | ||
+ | <?php | ||
- | // Function splitter | + | |
- | // Parameter: a string of text containing words and punctuation | + | // Parameter: a string of text containing words and punctuation |
- | // Returns: an array in which the unique words of the string are | + | // Returns: an array in which the unique words of the string are |
- | // the keys and their frequencies are the values. | + | // the keys and their frequencies are the values. |
- | function splitter($str) { | + | |
- | // Create the empty word frequency array | + | |
- | | + | |
- | // Split the parameter string into words | + | |
- | | + | |
- | // Loop to count the words (either increment or initialize to 1) | + | |
- | | + | |
- | $keys = array_keys($freq); | + | $keys = array_keys($freq); |
- | if(in_array($word, | + | if (in_array($word, |
- | $freq[$word]++; | + | $freq[$word]++; |
- | else | + | else |
- | $freq[$word] = 1; | + | $freq[$word] = 1; |
- | } | + | } |
- | | + | |
- | } #** End of splitter | + | }#** End of splitter |
- | // Main test driver | + | |
- | | + | |
or maybe you like oranges better than apples"; | or maybe you like oranges better than apples"; | ||
- | // Call splitter | + | |
- | | + | |
- | // Display the words and their frequencies | + | |
- | | + | |
- | $sorted_keys = array_keys($tbl); | + | $sorted_keys = array_keys($tbl); |
- | sort($sorted_keys); | + | sort($sorted_keys); |
- | foreach ($sorted_keys as $word) { | + | foreach ($sorted_keys as $word) { |
- | print "$word $tbl[$word] <br />"; | + | print "$word $tbl[$word] <br />"; |
- | } | + | } |
- | ?> | + | ?> |
- | </ | + | </ |
- | </ | + | </ |
</ | </ | ||
eg-259/lecture17.txt · Last modified: 2013/03/08 18:04 by eechris