Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 DIG 3134 - Lecture 2: Rapid Introduction To PHP Programming OR Review: Variables and Functions Michael Moshell University of Central Florida Media Software.

Similar presentations


Presentation on theme: "1 DIG 3134 - Lecture 2: Rapid Introduction To PHP Programming OR Review: Variables and Functions Michael Moshell University of Central Florida Media Software."— Presentation transcript:

1 1 DIG 3134 - Lecture 2: Rapid Introduction To PHP Programming OR Review: Variables and Functions Michael Moshell University of Central Florida Media Software Design

2 2 The Objectives: Acquire/refresh key programming ideas Learn how to debug a small program

3 3 The Strategy: Start slow, make sure you got it 1:30

4 4 The Strategy: Start slow, make sure you got it Accelerate rapidly Lose 25% of you 2:15 1:30

5 5 The Strategy: Start slow, make sure you got it Accelerate rapidly Lose 25% of you See (some of) you in office 2:152:45 1:30

6 6 The Strategy: Start slow, make sure you got it Accelerate rapidly Lose 25% of you See (some of) you in office Drop/ add Thursday 2:152:45 1:30

7 7 Concept (-1): "Hello World" The first program you write in a new language: <?php print "Hello world!"; ?>

8 8 Concept 0: PHP in HTML PHP is an embedded language. It can exist INSIDE an HTML document. Welcome to the rd Reunion!

9 99 But Remember.. Javascript is also an embedded language, BUT Javascript is executed by the CLIENT (Browser) PHP is executed by the SERVER (Internet host) This has MANY consequences, as we will see. -: Less immediate interaction than Javascript +: More control, reliability, centralized development *: Ajax combines BOTH (client and server-side)

10 10 Concept 1: Data types Numbers: - Integers 0, 2, 4, 8 - Floating point 2.45, -888.99 [no real distinction in PHP]

11 11 Concept 1: Data types Numbers: - Integers 0, 2, 4, 8 - Floating point 2.45, -888.99 [no real distinction in PHP] Strings (of characters) -’Joe Wilson’ -”My left foot” -”3, 47, 238, 222.44”

12 12 Concept 1: Data types Numbers: - Integers 0, 2, 4, 8 - Floating point 2.45, -888.99 [no real distinction in PHP] Strings (of characters) -’Joe Wilson’ -”My left foot” -”3, 47, 238, 222.44” Boolean: true, false

13 13 Concept 1: Data types PHP plays ‘fast and loose’ with types. You can mix and match them, and it Does the best it can to figure out your Meaning. Examples coming soon.

14 14 Concept 2: Variables (simple) Think of a variable as a labelled box. $x Name of the Variable (always begins With $) Contents: Can be number, String or Boolean. The CONTENTS of all the variables in a program Represent its STATE or MEMORY.

15 15 Concept 3: Assignments,Constants Putting something into a variable: $x Before the assignment: After the assignment: $x = 47; $x 47 The computer executes this statement, or command.

16 16 Concept 3: Assignments,Constants Putting something into a variable: $x Before the assignment: After the assignment: $x = 47; $x 47 The computer executes this statement, or command. That’s a constant (something that doesn’t change.)

17 17 Concept 3: Assignment Putting something ELSE into a variable: $x Before the assignment: $x = 321;The computer executes this statement, or command. 47

18 18 Concept 3: Assignment Putting something ELSE into a variable: $x Before the assignment: After the assignment: $x = 321; $x 321 The computer executes this statement, or command. 47 The previous contents are entirely replaced.

19 19 Concept 4: Expressions In an Assignment: The Expression on the RIGHT is computed and then it is put into the variable. $x = 33 + 22; $x 55 First compute 33+22, yields 55 Then dump it into $x.

20 20 About Variable Names Safest names are $ plus text-strings, like $price; You can do a lot of other stuff in php, like $_this; But don’t do $$this – it has VERY confusing consequences.

21 21 Concept 4: Expressions The ‘=‘ is an EVIL TRICKY SYMBOL. It really should be an arrow like  Because it does NOT mean “equals!” It means “evaluate right, copy to left”. pronounce: "is replaced by the value of..." $x = 33 + $x; $x 55 $x ? Before executionAfter execution

22 22 Concept 4: Expressions The ‘=‘ is an EVIL TRICKY SYMBOL. It really should be an arrow like  Because it does NOT mean “equals!” It means “evaluate right, copy to left”. $x = 33 + $x; $x 55 $x 88 Before executionAfter execution

23 23 Class practice 1: (lucky Volunteer) Pronounce this Line of PHP code for me please: $daynumber = 10 + $daynumber;

24 24 Class practice 2: (lucky Volunteer) What will be the contents Of $daynumber AFTER this executes? $daynumber = 10 + $daynumber; Before executionAfter execution $daynumber 4

25 25 Class practice 2: (lucky Volunteer) What will be the contents Of $daynumber AFTER this executes? $daynumber = 10 + $daynumber; Before executionAfter execution $daynumber 4 14

26 26 Concept 5: Hand Simulation Acting out what the program will do. Your job: Answer question at bottom. $price=3; $quantity=4; $cost=$price*$quantity; Print $cost; What is printed? $price $quantity $cost

27 27 Concept 5: Hand Simulation Use an actual marker on the side whiteboard. Show how to do the hand simulation.

28 28 Practice: Hand Simulation EVERYBODY needs Pen(cil) & paper! Do this! You have 60 seconds. $x = 2; $y = 3*$x -4; $z= 3-$y; $y = $y + $z; Print $y; $z What is printed? (PAUSE while they work.) $x $y

29 29 Practice: Hand Simulation Acting out what the program will do. Your job: Answer question at bottom. $x = 2; $y = 3*$x -4; $z= 3-$y; $y = $y + $z; Print $y; $z What is printed? $x $y 2

30 30 Practice: Hand Simulation Acting out what the program will do. Your job: Answer question at bottom. $x = 2; $y = 3*$x -4; $z= 3-$y; $y = $y + $z; Print $y; $z What is printed? $x $y 2 2

31 31 Practice: Hand Simulation Acting out what the program will do. Your job: Answer question at bottom. $x = 2; $y = 3*$x -4; $z= 3-$y; $y = $y + $z; Print $y; $z What is printed? $x $y 2 2 1

32 32 Practice: Hand Simulation Acting out what the program will do. Your job: Answer question at bottom. $x = 2; $y = 3*$x -4; $z= 3-$y; $y = $y + $z; Print $y; $z What is printed?3 is printed. $x $y 2 2 1 3

33 33 Why Do Hand Simulation? If you don't know what your code SHOULD BE GOING, How the heck are you going to verify That it IS DOING IT? You will soon develop 'mental models' of The computer's operation. But at the Beginning, you need an external model And that's the stuff on the paper.

34 34 Concept 6: Comments Human-readable reminders in the code. Three ways to make them: # this hides the rest of the line // and so does this

35 35 Concept 6: Comments Human-readable reminders in the code. Three ways to make them: # this hides the rest of the line // and so does this /* this hides everything until you do its opposite like this */

36 36 Concept 7: Logical expressions and code blocks $cost=43;$limit=$100; // and then later in the program:: if ($cost<=$limit) { print ‘This is a legal expenditure!’; } else { print ‘No! Too much money!’; }

37 37 Concept 7: Logical expressions and code blocks $cost=43;$limit=$100; // and then later in the program:: if ($cost<=$limit) { print ‘This is a legal expenditure!’; } else { print ‘No!’; } Logical Expression (T/F)

38 38 Concept 7: Logical expressions and code blocks $cost=43;$limit=$100; // and then later in the program:: if ($cost<=$limit) { print ‘This is a legal expenditure!’; } else { print ‘No!’; } Logical Expression (T/F) Code block

39 39 Aside: PHP cares about Case! $Cost Is not the same as $cost

40 40 Concept 8: Loops Computers are useless for doing things ONCE. $k=1; While ($k<100) { print $k; $k=2*$k; } What will this print?

41 41 Concept 8: Loops Computers are useless for doing things ONCE. $k=1; While ($k<100) { print $k; $k=2*$k; } What will this print?If you don't know, then Hand simulate it! (We do it on the board.)

42 42 Aside: What will it LOOK like? Remember the output goes to a browser. $k=1; While ($k<100) { print $k; $k=2*$k; } What will this print? 1 2 4 8 16 32 64 Use Dreamweaver & MAMP to show how this works.

43 43 Aside: What will it LOOK like? Remember the output goes to a browser. $k=1; While ($k<100) { print $k.”\n”; $k=2*$k; } What will this print?

44 44 Aside: What will it LOOK like? Remember the output goes to a browser. $k=1; While ($k<100) { print $k.”\n”; $k=2*$k; } What will this print? 2 4 8 16 32 64 BUT – let’s VIEW SOURCE.

45 45 Aside: What will it LOOK like? To make it react as if to HTML: $k=1; While ($k<100) { print $k.” ”; $k=2*$k; } What will this print?

46 46 Aside: What will it LOOK like? To make it react as if to HTML: $k=1; While ($k<100) { print $k.” ”; $k=2*$k; } What will this print? 2 4 8 16 32 64

47 47 Aside: What will it LOOK like? To make it react as if to HTML: $k=1; While ($k<100) { print $k. ” ”; $k=2*$k; } We concatenate a NUMBER $k and a STRING “ ”.. It becomes a string. We dissect This thing. DOT (.) means string Concatenation (“stick ‘em together”)

48 48 PHP Errors Let’s sabotage the program. Omit a semicolon $n=1 While ($n<100) { print $n.”\n”; $n=2*$n; }.. And see what happens. Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\little.php on line 6

49 49 Aside: TABLES in HTML Before CSS, people used tables to lay out pages. > Today, many are taught "Tables are EVIL". False!

50 50 Aside: TABLES in HTML Before CSS, people used tables to lay out pages. > Today, many are taught "Tables are EVIL". False. Tables are perfect for tabular data. You can use CSS to make 'em look good. Here's a quick review of HTML Tables.

51 51 Aside: TABLES in HTML Actor Movie Marlon Brando A Streetcar Named Desire Tobey Maguire Spiderman

52 52 Aside: TABLES in HTML

53 53 What will it LOOK like? Let’s try to make a nice table appear. Print " "; $n=1; While ($n<100) { print " ".$n. " "; $n=2*$n; } Print " "; Use MAMP to run this code for the class. 'tabledemo1.php'.

54 54 A microsoft GOTCHA! So-called “Smart Quotes” don’t work in PHP. You need DUMB QUOTES like " Print " " ; $n=1; While ($n<100) { print " ".$n. " " ; $n=2*$n; } Print " " ;

55 55 Practice emitting HTML 1.Make this table appear! rows 1, 4, 8 2. Make this table appear. rows 2, 5, 9 3. How about this one? (** appears whenever the contents exceed 16. Use an IF inside a loop) 3, 6, 10 4. For the advanced among you:

56 56 Practice emitting HTML ASSIGNMENT 1: Whichever projects your row DIDN'T COMPLETE, you are to complete them by next class. Somebody (bodies) will be called upon to show their work, And if you miss the prof points, they're GONE. So BE PREPARED. If you can't do it, come see me or get help. Now, we move to Dessert: Game Zero; Acey-Ducey

57 57 An OPTIONAL Opportunity ASSIGNMENT 1a: If your team includes people with Significant programming experience (there are a few) And you can do it in PHP (for carryforward benefit) Then program the "guessing game": Software randomly generates a number N between 1 – 100. Asks user for a guess. If the guess is <N print out "too low!" Else if the guess is >N print out "too high" Else print out "You got it!" (extra: count the # of tries!)


Download ppt "1 DIG 3134 - Lecture 2: Rapid Introduction To PHP Programming OR Review: Variables and Functions Michael Moshell University of Central Florida Media Software."

Similar presentations


Ads by Google