2010/11 : [1]building web applications using mysql and php (w1)php recap

41
2010/11 : [1] Building Web Applications using MySQL and PHP (W1) PHP Recap PHP Recap

Upload: daisy-rogers

Post on 26-Dec-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

PHP Recap

Page 2: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [2]Building Web Applications using MySQL and PHP (W1)PHP Recap

We need to remember..

• Variables, arrays.

• Control structures. Logic.

• Functions.

Page 3: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [3]Building Web Applications using MySQL and PHP (W1)PHP Recap

The manual will help!

• If in doubt, refer to the PHP manual.

www.php.net

Page 4: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [4]Building Web Applications using MySQL and PHP (W1)PHP Recap

Fundamentals

• PHP is embedded within xhtml pages within the tags: <?php ... ?>• The short version of these tags can also be

used: <? ... ?>, but should be avoided as most servers don’t support them• Each line of PHP is terminated with a

semi-colon.

Page 5: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [5]Building Web Applications using MySQL and PHP (W1)PHP Recap

Comments

<?php

// this is a comment

echo ‘Hello World!’;

/* another

multi-line comment */

?>

Page 6: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [6]Building Web Applications using MySQL and PHP (W1)PHP Recap

Variables

• $ followed by a variable name<?php$name = ‘Phil’;$age = 23;echo $name;echo ’ is ‘;echo $age;// Phil is 23?>

Page 7: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [7]Building Web Applications using MySQL and PHP (W1)PHP Recap

" or '

• There is a difference between strings written in single and double quotes.

• In a double-quoted string any variable names are expanded to their values.

• In a single-quoted string, no variable expansion takes place.

Page 8: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [8]Building Web Applications using MySQL and PHP (W1)PHP Recap

" or '

<?php

$name = ‘Phil’;

$age = 23;

echo “$name is $age”;

// Phil is 23

?>

Page 9: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [9]Building Web Applications using MySQL and PHP (W1)PHP Recap

Expressions

Using variables within expressions to do something is what PHP is all about.

<?php

$name = ‘Ian’;

echo $name;

?>

Expression

Operator

Page 10: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [10]Building Web Applications using MySQL and PHP (W1)PHP Recap

String Operators

Use a dot to concatenate two strings:

e.g.

$firstname = ‘Gerard’;

$surname = ‘Luskin’;

// displays ‘Gerard Luskin’

echo $firstname.’ ‘.$surname;

Page 11: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [11]Building Web Applications using MySQL and PHP (W1)PHP Recap

Arithmetic Operators

Example Name Result

$a + $b Addition Sum of $a and $b.

$a - $b Subtraction Difference of $a and $b.

$a * $b Multiplication Product of $a and $b.

$a / $b Division Quotient of $a and $b.

$a % $b Modulus Remainder of $a divided by $b.

Page 12: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [12]Building Web Applications using MySQL and PHP (W1)PHP Recap

Comparison Operators

Example Name Result

$a == $b Equal TRUE if $a is equal to $b.

$a != $b Not equal TRUE if $a is not equal to $b.

$a <> $b Not equal TRUE if $a is not equal to $b.

$a < $b Less than TRUE if $a is strictly less than $b.

$a > $b Greater than TRUE if $a is strictly greater than $b.

$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.

$a >= $b Gtr than or equal to TRUE if $a is greater than or equal to $b.

Page 13: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [13]Building Web Applications using MySQL and PHP (W1)PHP Recap

Incrementing/Decrementing

Example Name Effect$a++ Post-increment Returns $a, then increments $a by one.

$a-- Post-decrement Returns $a, then decrements $a by one.

You might also see these, but we strongly recommend that you don’t use them:

--$a Pre-decrement Decrements $a by one, then returns $a.

++$a Pre-increment Increments $a by one, then returns $a.

Page 14: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [14]Building Web Applications using MySQL and PHP (W1)PHP Recap

Logical Operators

Example Name Result$a and $b And TRUE if both $a and $b are TRUE.

$a or $b Or TRUE if either $a or $b is TRUE.

$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.

!$a Not TRUE if $a is not TRUE.

$a && $b And TRUE if both $a and $b are TRUE.

$a || $b Or TRUE if either $a or $b is TRUE.

Page 15: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [15]Building Web Applications using MySQL and PHP (W1)PHP Recap

An array

Normal Variable, no key:

$name = ‘Gerard’;

Array Variable, multiple pieces with ‘keys’:

$name[0] = ‘Gerard’;

$name[1] = ‘Dionisis’;

$name[2] = ‘Ian’;

…The ‘key’

Page 16: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [16]Building Web Applications using MySQL and PHP (W1)PHP Recap

Array keys

Array keys can be strings as well as numbers..

$surname[‘gerard’] = ‘Luskin’;$surname[‘ian’] = ‘Harrison’;

Notice the way that the key is specified, in square brackets following the variable name.

Page 17: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [17]Building Web Applications using MySQL and PHP (W1)PHP Recap

Working with arrays..

Create Array (automatic keys):$letters = array('a','b','c','d');

The array keys are automatically assigned by PHP as 0, 1, 2, 3

i.e. $letters[1] has value ‘b’

Create Array (explicit keys):

$letters = array(10=>’a’,13=>’b’);

i.e. $letters[13] has value ‘b’

Page 18: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [18]Building Web Applications using MySQL and PHP (W1)PHP Recap

Working with arrays…

Create array (component by component):$letters[10] = ‘a’;

$letters[13] = ‘b’;

Access array component:echo $letters[10];

// displays a

echo $letters[10].$letters[13];

// displays ab

Page 19: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [19]Building Web Applications using MySQL and PHP (W1)PHP Recap

Working with arrays…

Note that trying to echo an entire array will not display the data. To print an entire

array to screen (for debug, for example) use the function print_r instead.

echo $letters;

print_r($letters);

Page 20: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [20]Building Web Applications using MySQL and PHP (W1)PHP Recap

If …

To do something depending on a comparison, use an if statement.

if (comparison)

{

expressions; // do if TRUE

}

NB: Notice the curly brackets – these are important!

Page 21: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [21]Building Web Applications using MySQL and PHP (W1)PHP Recap

If example

<?php

$a = 10;

$b = 13;

if ($a<$b)

{

echo ‘<p>a is smaller than b</p>’;

}

?>

Page 22: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [22]Building Web Applications using MySQL and PHP (W1)PHP Recap

Extending IF statements

It is possible to add extra optional clauses to if statements..if (comparison) {

expressions; // do if TRUE} else {

expressions; // do otherwise}

Page 23: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [23]Building Web Applications using MySQL and PHP (W1)PHP Recap

Extending IF statementsif (comparison1) {

expressions;} elseif (comparison2) {

expressions; } else {

expressions;}

Page 24: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [24]Building Web Applications using MySQL and PHP (W1)PHP Recap

An example..$a = 10;$b = 13;echo ‘<p>’;if ($a<$b) {

echo ‘a is smaller than b’;} elseif ($a==$b) {

echo ‘a is equal to b’;} else {

echo ‘a is bigger than b’;}echo ‘</p>’;

Page 25: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [25]Building Web Applications using MySQL and PHP (W1)PHP Recap

While loops

Might want to do something repeatedly while a comparison is true..

while (comparison) {expressions;

}

Page 26: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [26]Building Web Applications using MySQL and PHP (W1)PHP Recap

Example

Lets count to 10! Displays 1,2,3,4,5,..,10:

$i = 1;

while ($i <= 10) {   echo $i++;

}

Page 27: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [27]Building Web Applications using MySQL and PHP (W1)PHP Recap

For loopSometimes we want to loop around the same bit of code a number of times.. Use a for loop.

for (expr1; expr2; expr3) {

statements; }

expr1 evaluated/executed initiallyexpr2 evaluated at beginning of each iteration

(Continues if TRUE)expr3 evaluated/executed at end of each

iteration

Page 28: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [28]Building Web Applications using MySQL and PHP (W1)PHP Recap

For loop example

To count from 1 to 10:

for ($i=1; $i<=10; $i++) {

echo $i;

}

initialiseContinue if true

Execute at end of loop

Page 29: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [29]Building Web Applications using MySQL and PHP (W1)PHP Recap

Foreach loop

A foreach loop is designed for arrays. Often you want to loop through each item in an array in turn..

$letters = array(‘a’,’b’,’c’);

foreach ($letters as $value) {   echo $value;

} // outputs a,b,c in turn

Page 30: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [30]Building Web Applications using MySQL and PHP (W1)PHP Recap

require, include

require('filename.ext')Includes and evaluates the specified file

Error is fatal (will halt processing)

include('filename.ext')Includes and evaluates the specified file

Error is a warning (processing continues)

require_once / include_onceIf already included won’t be included again

Page 31: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [31]Building Web Applications using MySQL and PHP (W1)PHP Recap

Code Re-use

Often you will want to write a piece of code and re-use it several times (maybe within the same script, or maybe between different scripts).

Functions are a very nice way to encapsulate such pieces of code..

Page 32: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [32]Building Web Applications using MySQL and PHP (W1)PHP Recap

What is a function?

A function takes some arguments (inputs) and does something with them (echo, for example, outputs the text input to the user).

As well as the inbuilt PHP functions, we can define our own functions..

Page 33: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [33]Building Web Applications using MySQL and PHP (W1)PHP Recap

Definition vs. Calling

There are two distinct aspects to functions:

Definition: Before using a function, that function must be defined – i.e. what inputs does it need, and what does it do with them?

Calling: When you call a function, you actually execute the code in the function.

Page 34: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [34]Building Web Applications using MySQL and PHP (W1)PHP Recap

Function Definition

A function accepts any number of input arguments, and returns a SINGLE value.

function myfunction($arg1,$arg2,…,$argN)

{

statements;

return $return_value;

}

Page 35: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [35]Building Web Applications using MySQL and PHP (W1)PHP Recap

Example

Function to join first and last names together with a space..

function make_name($first,$last)

{

$fullname = $first.’ ‘.$last;

return $fullname;

}

Page 36: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [36]Building Web Applications using MySQL and PHP (W1)PHP Recap

Calling functions..

Can be done anywhere..myfunction($arg1,$arg2,…,$argN)

or$answer = myfunction($arg1,$arg2,…,$argN)

e.g.

echo make_name(‘Gerard’,’Luskin’);

// echoes ‘Gerard Luskin’

Page 37: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [37]Building Web Applications using MySQL and PHP (W1)PHP Recap

Functions: Return Values

Use return()Causes execution of function to cease

Control returns to calling script

To return multiple valuesReturn an array

If no value returnedNULL

Page 38: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [38]Building Web Applications using MySQL and PHP (W1)PHP Recap

‘Scope’A function executes within its own little

protected bubble, or local scope.

What does this mean? Its means that the function can’t ‘see’ any of the variables you have defined apart from those passed in as arguments..

Each new function call starts a clean slate in terms of internal function variables.

Page 39: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [39]Building Web Applications using MySQL and PHP (W1)PHP Recap

In other words..Variables within a function

Are local to that functionDisappear when function execution ends

Variables outside a functionAre not available within the function

Unless set as global

Remembering variablesNot stored between function calls

Unless set as static

Page 40: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [40]Building Web Applications using MySQL and PHP (W1)PHP Recap

Exercise

Hands On Exercise

PHP Recap: Procedural Code

Page 41: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [41]Building Web Applications using MySQL and PHP (W1)PHP Recap

Review

• Recapped variables, arrays.• Recapped control structures, logic.• Recapped functions.• For more, look back at your notes from the

P1 course and consult the PHP manual.