1 dig 3134 - lecture 2: rapid introduction to php programming or review: variables and functions...

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

Upload: victor-wheeler

Post on 03-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

1

DIG 3134 - Lecture 2:

Rapid Introduction To PHP Programming

ORReview: Variables and Functions

Michael MoshellUniversity of Central Florida

Media Software Design

2

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

3

The Strategy:• Start slow, make sure you got it

1:30

4

The Strategy:• Start slow, make sure you got it• Accelerate rapidly• Lose 25% of you

2:151:30

5

The Strategy:• Start slow, make sure you got it• Accelerate rapidly• Lose 25% of you• See (some of)

you in office

2:15 2:451:30

6

The Strategy:• Start slow, make sure you got it• Accelerate rapidly• Lose 25% of you• See (some of)

you in office

Drop/add

Thursday2:15 2:451:30

7

Concept (-1): "Hello World"

The first program you write in a new language:<?php

print "Hello world!";?>

8

Concept 0: PHP in HTML

PHP is an embedded language.It can exist INSIDE an HTML document.

<html><body>

<?php $x=43; ?>

<h2> Welcome to the <?php print $x; ?>rd Reunion!</h2>

</body></html>

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

Concept 1: Data types

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

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

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

Concept 1: Data types

PHP plays ‘fast and loose’ with types.

You can mix and match them, and itDoes the best it can to figure out yourMeaning.

Examples coming soon.

14

Concept 2: Variables (simple)

Think of a variable as a labelled box.

$x

Name of theVariable(always beginsWith $)

Contents:Can be number,String or Boolean.

The CONTENTS of all the variables in a programRepresent its STATE or MEMORY.

15

Concept 3: Assignments,Constants

Putting something into a variable:

$x Before the assignment:

After the assignment:

$x = 47;

$x 47

The computer executes thisstatement, or command.

16

Concept 3: Assignments,Constants

Putting something into a variable:

$x Before the assignment:

After the assignment:

$x = 47;

$x 47

The computer executes thisstatement, or command.

That’s a constant (something that doesn’t change.)

17

Concept 3: Assignment

Putting something ELSE into a variable:

$x Before the assignment:

$x = 321; The computer executes thisstatement, or command.

47

18

Concept 3: Assignment

Putting something ELSE into a variable:

$x Before the assignment:

After the assignment:

$x = 321;

$x 321

The computer executes thisstatement, or command.

47

The previous contents are entirely replaced.

19

Concept 4: Expressions

In an Assignment:The Expression on the RIGHT is computedand then it is put into the variable.

$x = 33 + 22;

$x 55

First compute 33+22, yields 55Then dump it into $x.

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

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 execution After execution

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 execution After execution

2323

Class practice 1:

(lucky Volunteer) Pronounce thisLine of PHP code for me please:

$daynumber = 10 + $daynumber;

242424

Class practice 2:

(lucky Volunteer) What will be the contentsOf $daynumber AFTER this executes?

$daynumber = 10 + $daynumber;

Before execution After execution

$daynumber $daynumber 4

25252525

Class practice 2:

(lucky Volunteer) What will be the contentsOf $daynumber AFTER this executes?

$daynumber = 10 + $daynumber;

Before execution After execution

$daynumber $daynumber 4 14

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

Concept 5: Hand Simulation

Use an actual marker on the side whiteboard.

Show how to do the hand simulation.

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

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

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

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

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

3333

Why Do Hand Simulation?

If you don't know what your codeSHOULD BE GOING,

How the heck are you going to verify That it IS DOING IT?

You will soon develop 'mental models' ofThe computer's operation. But at theBeginning, you need an external modelAnd that's the stuff on the paper.

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

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 everythinguntil you do its opposite like this */

36

Concept 7: Logical expressionsand 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

Concept 7: Logical expressionsand code blocks

$cost=43; $limit=$100;// and then later in the program::

if ($cost<=$limit){

print ‘This is a legal expenditure!’;} else{

print ‘No!’;}

LogicalExpression(T/F)

38

Concept 7: Logical expressionsand code blocks

$cost=43; $limit=$100;// and then later in the program::

if ($cost<=$limit){

print ‘This is a legal expenditure!’;} else{

print ‘No!’;}

LogicalExpression(T/F)

Code

block

39

Aside: PHP cares about Case!

$CostIs not the same as$cost

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?

4141

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, thenHand simulate it! (We do it on the board.)

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 64Use Dreamweaver & MAMP to show how this works.

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

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 64BUT – let’s VIEW SOURCE.

45

Aside: What will it LOOK like?

To make it react as if to HTML:$k=1;While ($k<100){

print $k.”<br />”;$k=2*$k;

}

What will this print?

46

Aside: What will it LOOK like?

To make it react as if to HTML:$k=1;While ($k<100){

print $k.”<br />”;$k=2*$k;

}

What will this print?

248163264

47

Aside: What will it LOOK like?

To make it react as if to HTML:$k=1;While ($k<100){

print $k.”<br />”;

$k=2*$k;}

We concatenate a NUMBER$k and a STRING “<br />” .. It becomes a string.

We dissectThis thing.DOT (.) meansstring Concatenation(“stick ‘em together”)

48

PHP Errors

Let’s sabotage the program. Omit a semicolon

$n=1While ($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

4949

Aside: TABLES in HTML

Before CSS, people used tables to lay out pages.<< Bad idea, but we didn't have alternatives.>>

Today, many are taught "Tables are EVIL".False!

505050

Aside: TABLES in HTML

Before CSS, people used tables to lay out pages.<< Bad idea, but we didn't have alternatives.>>

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.

515151

Aside: TABLES in HTML

<table border=1><tr><th>Actor</th><th>Movie</th></tr><tr>

<td>Marlon Brando</td><td>A Streetcar Named Desire</td>

</tr><tr>

<td>Tobey Maguire</td><td>Spiderman</td>

</tr></table>

52

Aside: TABLES in HTML

53

What will it LOOK like?

Let’s try to make a nice table appear.

Print "<table border=1>";$n=1;While ($n<100){

print "<tr><td>".$n. "</td></tr>";$n=2*$n;

}Print "</table>";Use MAMP to run this code for the class.

'tabledemo1.php'.

54

A microsoft GOTCHA!

So-called “Smart Quotes” don’t work in PHP.You need DUMB QUOTES like "

Print " <table border=1> ";$n=1;While ($n<100){

print " <tr><td> ".$n. " </td></tr> ";$n=2*$n;

}Print " </table> ";

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 thecontents exceed 16. Use an IF inside a loop)

3, 6, 10

4. For the advanced among you:

5 10 15 20 25

1 2 3 4 56 7 8 9 10

5 10 15 ** **

Multiplication table2 3 4 5

2 4 6 8 103 6 9 12 154 8 12 16 205 10 14 20 25

5656

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 BEPREPARED.If you can't do it, come see me or get help.

Now, we move to Dessert: Game Zero; Acey-Ducey

575757

An OPTIONAL Opportunity

ASSIGNMENT 1a: If your team includes people withSignificant 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!)