Mdst 3559-02-17-php2
-
Upload
rafael-alvarado -
Category
Technology
-
view
205 -
download
0
description
Transcript of Mdst 3559-02-17-php2
PHP 2
MDST 3559: DataestheticsProf. Alvarado
02/17/2011
Business
• VPN?
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,
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
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
What are some elements of natural language?
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
PHP has all of these things
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!”5true
• These are the meanings of the language
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
(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 ...
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
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 effectsPerson A: He ran for office.Person B: And ... ?
Expressions• Expressions are like phrases that combine nouns and
verbs5(5 + 10) / 36“Tina” . “ is my cat”;file($url)$foo
• All expressions result in a value50.4166666 ...Tina is my cat[the array of the file][whatever $foo was last set to]
Statements
• Statements are like sentences. They combine one or more expressions.$my_cat = “Tina”;$x = 2+2;
• They say something about somethingMy Cat is Tina.$foo is 2+2 (is 5)The verb “to be” is an assignment operator ...
• They are always punctuated by a semi-colon
Operators
• Expressions and statements are built out of words by combining them with grammatical words, such as prepositions and conjunctionsI am in the house.I own a cat and a dog.
• Operators are like the grammatical wordsThey don’t have meanings, they have functionsThey don’t reference values (i.e. data), they do
things with values
Operators
• ArithmeticAddition, subtraction, division, multiplication, etc.
• StringConcatenation
• AssignmentX is Y
• LogicalIs X Y?Is X related to Y in some specified way?
Structures
• Structures, or control structures, are like stories or narrative patterns
• Built-in functions control conditions and iteration– Repetition and branching
Conditions
• if / elseif/ elseif ($x == ‘foo’) {
// do something} elseif ($x == ‘bar’) {
// do something else } else {
// do something else}
Iteration
• foreach()• while()
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
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
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
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
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