mdst 3559-02-17-php2

26
PHP 2 MDST 3559: Dataesthetics Prof. Alvarado 02/17/2011

Upload: rafael-alvarado

Post on 18-Dec-2014

204 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Mdst 3559-02-17-php2

PHP 2

MDST 3559: DataestheticsProf. Alvarado

02/17/2011

Page 2: Mdst 3559-02-17-php2

Business

• VPN?

Page 3: Mdst 3559-02-17-php2

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,

Page 4: Mdst 3559-02-17-php2

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

Page 5: Mdst 3559-02-17-php2

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

Page 6: Mdst 3559-02-17-php2

What are some elements of natural language?

Page 7: Mdst 3559-02-17-php2
Page 8: Mdst 3559-02-17-php2

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

Page 9: Mdst 3559-02-17-php2

PHP has all of these things

Page 10: Mdst 3559-02-17-php2

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

Page 11: Mdst 3559-02-17-php2

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

Page 12: Mdst 3559-02-17-php2

(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 ...

Page 13: Mdst 3559-02-17-php2

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

Page 14: Mdst 3559-02-17-php2

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 ... ?

Page 15: Mdst 3559-02-17-php2

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]

Page 16: Mdst 3559-02-17-php2

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

Page 17: Mdst 3559-02-17-php2

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

Page 18: Mdst 3559-02-17-php2

Operators

• ArithmeticAddition, subtraction, division, multiplication, etc.

• StringConcatenation

• AssignmentX is Y

• LogicalIs X Y?Is X related to Y in some specified way?

Page 19: Mdst 3559-02-17-php2

Structures

• Structures, or control structures, are like stories or narrative patterns

• Built-in functions control conditions and iteration– Repetition and branching

Page 20: Mdst 3559-02-17-php2

Conditions

• if / elseif/ elseif ($x == ‘foo’) {

// do something} elseif ($x == ‘bar’) {

// do something else } else {

// do something else}

Page 21: Mdst 3559-02-17-php2

Iteration

• foreach()• while()

Page 22: Mdst 3559-02-17-php2

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

Page 23: Mdst 3559-02-17-php2

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

Page 24: Mdst 3559-02-17-php2

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

Page 25: Mdst 3559-02-17-php2

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

Page 26: Mdst 3559-02-17-php2

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