Mdst 3559-02-17-php2


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Mdst 3559-02-17-php2

  1. 1. PHP 2<br />MDST 3559: DataestheticsProf. Alvarado02/17/2011<br />
  2. 2. Business<br />VPN?<br />
  3. 3. Review<br />PHP<br />Variables, Data Types, Functions<br />See Chapters 2 through 5 of Programming PHP<br />Text<br />Earlier we saw the text handled as a tree (CSS, jQuery)<br />Tuesday we treated the text as an array, a simple linear sequence,<br />
  4. 4. Overview<br />Today we will go back over the basics of PHP <br />To provide a slightly different angle on the subject<br />To help clarify some concepts for those new to programming<br />To help clarify the “PHP way” for those with experience<br />Then we will cover two new topics<br />Regular Expressions<br />Creating your own functions<br />
  5. 5. PHP as a language<br />PHP is a programming langague (of course), but it has analogies to natural language as well<br />There are parallels to the basic structures of human languages<br />Which makes sense: <br />Programming languages are designed to mediate between human and machine language<br />
  6. 6. What are some elements of natural language?<br />
  7. 7.
  8. 8. Some Elements of Natural Language<br />Words<br />Meanings<br />Sentences<br />Something is said of something else<br />Parts of speech: nouns, verbs, prepositions<br />Heart of language as language, beyond mere signs<br />Paragraphs<br />
  9. 9. PHP has all of these things<br />
  10. 10. Values are Meanings<br />Values are pieces of data<br />Languages are all about doing things with data<br />Data comes in types<br />Types include strings and numbers<br />Also Boolean values (true/false)<br />Values are expressed literally and signified like so:<br />“Hello, World!”<br />5<br />true<br />These are the meanings of the language<br />
  11. 11. Variables are Nouns<br />Variables are words that can have meaning<br />$POTUS = “Obama”;<br />The President of the U.S. is Obama.<br />Like the difference between person and office <br />They are like nouns that refer to things <br />Things being values ...<br />They are always prefixed by $ and are case sensitive<br />
  12. 12. (Arrays)<br />Arrays are admittedly weird<br />But they can be thought of as words that stand for collections, e.g.<br />The Pittsburgh Steelers<br />The UVA student body<br />These phrases imply a structure<br />Albeit much more complicated than arrays<br />But not much: consider rosters ...<br />
  13. 13. Functions are Verbs<br />Functions are like verbs<br />$food = food_shop($me,$money,$humger_level)<br />Like verbs, they may require arguments and imply a result<br />Verbs are action words<br />Consider transitive versus intransitive<br />They are not prefixed by $<br />They are not case sensitive<br />They are followed by parentheses<br />
  14. 14. Functions<br />Verbs have implicit arguments and effects<br />Subjects, objects, indirect objects, are like arguments<br />Intransitive verbs are like functions that don’t take an argument<br />Or, it is implicit (the subject)<br />Functions like verbs imply actions that produce effects<br />Person A: He ran for office.<br />Person B: And ... ?<br />
  15. 15. Expressions<br />Expressions are like phrases that combine nouns and verbs<br />5<br />(5 + 10) / 36<br />“Tina” . “ is my cat”;<br />file($url)<br />$foo<br />All expressions result in a value<br />5<br />0.4166666 ...<br />Tina is my cat<br />[the array of the file]<br />[whatever $foo was last set to]<br />
  16. 16. Statements<br />Statements are like sentences. They combine one or more expressions.<br />$my_cat = “Tina”;<br />$x = 2+2;<br />They say something about something<br />My Cat is Tina.<br />$foois 2+2 (is 5)<br />The verb “to be” is an assignment operator ...<br />They are always punctuated by a semi-colon<br />
  17. 17. Operators<br />Expressions and statements are built out of words by combining them with grammatical words, such as prepositions and conjunctions<br />Iam in the house.<br />I own a catanda dog.<br />Operators are like the grammatical words<br />They don’t have meanings, they have functions<br />They don’t reference values (i.e. data), they do things with values<br />
  18. 18. Operators<br />Arithmetic<br />Addition, subtraction, division, multiplication, etc.<br />String<br />Concatenation<br />Assignment<br />X is Y<br />Logical<br />Is X Y?<br />Is X related to Y in some specified way?<br />
  19. 19. Structures<br />Structures, or control structures, are like stories or narrative patterns<br />Built-in functions control conditions and iteration<br />Repetition and branching <br />
  20. 20. Conditions<br />if / elseif/ else<br />if ($x == ‘foo’) {<br /> // do something<br />} elseif ($x == ‘bar’) {<br /> // do something else <br />} else { <br /> // do something else<br />}<br />
  21. 21. Iteration<br />foreach()<br />while()<br />
  22. 22. Summary<br />Values are like meanings<br />Variables are like nouns<br />Functions are like verbs<br />Operators are like grammatical words<br />These are combined to form expressions and statements<br />Statements are structured in stories, or algorithms with control structures<br />
  23. 23. Exercise<br />Create directory for today<br />Copy rex.php into it<br />Let’s revisit the program and use control structures to format the text<br />Use foreach() to loop through the file<br />Use IF statements to grab what we want<br />Format the text with tags<br />
  24. 24. Two new functions<br />preg_match($pattern, $string) <br />Two arguments<br />A regular expression pattern (between slashes “/.../”)<br />A string to apply the expression to<br />preg_replace($pattern,$replacement,$string)<br />Three arguments<br />A regular expression<br />A something to replace the matched string with<br />A string to apply the expression to<br />
  25. 25. Regular Expressions<br />. = any character<br />+ = one or more<br />* = 0 or more<br />^ = beginning of the string<br />$ = end of the string<br />[A-Za-z] = character set of all letters<br />() = something to be replaced<br />
  26. 26. Assignment<br />Grab the Oedipux text <br />Trim the leading and trailing HTML tags<br />Reformat each line using either DIV or P elements<br />Create an appropriate CSS stylesheet for the text<br />