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