58
5-May-2007 : [1] BBK P1 Module Session 1 & 2 PHP: Moving On..

Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

Embed Size (px)

Citation preview

Page 1: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [1]BBK P1 ModuleSession 1 & 2

PHP: Moving On..

Page 2: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [2]BBK P1 ModuleSession 1 & 2

Reminder

• PHP is embedded within xhtml pages within the tags: <?php … ?>

• The short version of these tags can also be used: <? … ?>

• Each line of PHP is terminated, like MySQL, with a semi-colon.

Page 3: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [3]BBK P1 ModuleSession 1 & 2

Reminder

• Variables are automatically initialised when you start to use them.

e.g.

<?php

$name = ‘Rob’;

echo $name;

?>

Page 4: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [4]BBK P1 ModuleSession 1 & 2

Expressions

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

<?php

$name = ‘Rob’;

echo $name;

?>

Expression

Operator

Page 5: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [5]BBK P1 ModuleSession 1 & 2

Some Types of Operator

• Arithmetic

• Assignment

• Bitwise

• Comparison

• Ternary

• Incrementing/decrementing

• Logical• String

Page 6: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [6]BBK P1 ModuleSession 1 & 2

String Operators

• Use a dot to concatenate two strings:

e.g.

$firstname = ‘Rob’;

$surname = ‘Tuley’;

// displays ‘Rob Tuley’

echo $firstname.’ ‘.$surname;

Page 7: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [7]BBK P1 ModuleSession 1 & 2

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 8: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [8]BBK P1 ModuleSession 1 & 2

Assignment Operators

Example Result

$a = $b Sets $b to the same value as $a.

$a += $b Equivalent to $a = $a + $b.

$a .= $b Equivalent to $a = $a.$b.

Page 9: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [9]BBK P1 ModuleSession 1 & 2

Combining Operators

• Note that you can combine operators, for example use =, + and / in one expression:

$a = 4;

$b = 2;

$c = $a + $b + ($a/$b);

// $c has value 4+2+(4/2) = 8• Brackets help group operators.

Page 10: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [10]BBK P1 ModuleSession 1 & 2

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 11: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [11]BBK P1 ModuleSession 1 & 2

Comparisons

• Comparison expressions return a value of TRUE (or ‘1’) or FALSE (or ‘’).

e.g.

$a = 10;

$b = 13;

// result is true (‘1’)

echo $a < $b;

Page 12: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [12]BBK P1 ModuleSession 1 & 2

Incrementing/Decrementing

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

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

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

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

Page 13: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [13]BBK P1 ModuleSession 1 & 2

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 14: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [14]BBK P1 ModuleSession 1 & 2

Finally, a tricky one!

• A single ? is the ternary operator.

(expr) ? if_expr_true : if_expr_false;

• A test expression evaluates to TRUE or FALSE.

•TRUE gives first result (before colon)•FALSE gives second result (after colon)

Page 15: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [15]BBK P1 ModuleSession 1 & 2

Ternary Operator example

<?php

$a = 10;

$b = 13;

echo $a<$b ? ‘a smaller’:‘b smaller’;

// string ‘a smaller’ is echoed

// to the browser..

?>

Page 16: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [16]BBK P1 ModuleSession 1 & 2

Hands On Exercise 3

Expressions and Operators

Page 17: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [17]BBK P1 ModuleSession 1 & 2

Groups of variables

• So far, we have stored ONE piece of data in each variable.

• It is also possible to store multiple pieces of data in ONE variable by using an array.

• Each piece of data in an array has a key..

Page 18: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [18]BBK P1 ModuleSession 1 & 2

An array

Normal Variable, no key:

$name = ‘Rob’;

Array Variable, multiple pieces with ‘keys’:

$name[0] = ‘Rob’;

$name[1] = ‘Si’;

$name[2] = ‘Sarah’;

…The ‘key’

Page 19: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [19]BBK P1 ModuleSession 1 & 2

Array keys

• Array keys can be strings as well as numbers..

$surname[‘rob’] = ‘Tuley’;$surname[‘si’] = ‘Lewis’;

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

Page 20: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [20]BBK P1 ModuleSession 1 & 2

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 21: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [21]BBK P1 ModuleSession 1 & 2

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 22: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [22]BBK P1 ModuleSession 1 & 2

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 23: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [23]BBK P1 ModuleSession 1 & 2

So..We know we can:

1. Store things in named variables.

2. Use expressions to operate on the contents of these variables.

3. Can compare variables..

How do we actually include logic in the code such as ‘if this is bigger than that, do this’?

Page 24: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [24]BBK P1 ModuleSession 1 & 2

Control Structures

• if, elseif, else• while, do … while• for, foreach• switch• break, continue, return• require, include, require_once, include_once

Page 25: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [25]BBK P1 ModuleSession 1 & 2

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 26: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [26]BBK P1 ModuleSession 1 & 2

If example

<?php

$a = 10;

$b = 13;

if ($a<$b) {

echo ‘a is smaller than b’;

}

?>

Page 27: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [27]BBK P1 ModuleSession 1 & 2

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 28: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [28]BBK P1 ModuleSession 1 & 2

Extending If statements

if (comparison1) {

expressions;

} elseif (comparison2) {

expressions;

} else {

expressions;

}

Page 29: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [29]BBK P1 ModuleSession 1 & 2

An example..

$a = 10;$b = 13;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’;}

Page 30: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [30]BBK P1 ModuleSession 1 & 2

While loops

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

while (comparison) {expressions;

}

Page 31: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [31]BBK P1 ModuleSession 1 & 2

Example

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

$i = 1;

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

}

Page 32: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [32]BBK P1 ModuleSession 1 & 2

Do .. While

• An alternative...

$i = 1;

do {   echo $i++;

} while ($i <= 10);

Page 33: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [33]BBK P1 ModuleSession 1 & 2

For loop

• Sometimes 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 initially– expr2 evaluated at beginning of each

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

iteration

Page 34: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [34]BBK P1 ModuleSession 1 & 2

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 35: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [35]BBK P1 ModuleSession 1 & 2

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 36: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [36]BBK P1 ModuleSession 1 & 2

Foreach.. With keys

• Sometimes we want to use the array ‘key’ value too:

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

foreach ($letters as $key => $value) {   echo “array $key to $value”;

}

Page 37: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [37]BBK P1 ModuleSession 1 & 2

Switch statement

switch (expr) {

case (result1):

statements;

break;

case (result2):

statements;

break;

default:

statements;

}

• expr is evaluated– Case corresponding to

result is executed– Otherwise default case

is executed

• break– Ensures next case isn’t

executed

Page 38: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [38]BBK P1 ModuleSession 1 & 2

Switch Exampleswitch ($name) {

case ‘Rob’:

echo ‘Your name is Rob’;

break;

case ‘Fred’:

echo ‘You are called Fred’;

break;

default:

echo ‘Not sure what your name is’;

}

Page 39: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [39]BBK P1 ModuleSession 1 & 2

break, continue, return

• break– Ends execution of current for, foreach, do … while,

while or switch structure– Option: Number of nested structures to break out of

• continue– Skip rest of current loop– Option: Number of nested loops to skip

• return– Ends execution of current function/statement/script

Page 40: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [40]BBK P1 ModuleSession 1 & 2

Indentation..

• Code readability IS important – notice how all the code inside a loop/control structure is indented.

• Once you start writing nested control loops, indentation is the only way to keep track of your code!

Page 41: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [41]BBK P1 ModuleSession 1 & 2

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_once– If already included won’t be included again

Page 42: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [42]BBK P1 ModuleSession 1 & 2

Hands On Exercise 4

Control Structures

Page 43: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [43]BBK P1 ModuleSession 1 & 2

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 44: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [44]BBK P1 ModuleSession 1 & 2

Eh..? What?

• You have already used functions..

echo(‘text to display’);

Function NAME Function ARGUMENT

Page 45: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [45]BBK P1 ModuleSession 1 & 2

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 46: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [46]BBK P1 ModuleSession 1 & 2

Definition vs. Calling

There are two distinct aspects to functions:

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

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

Page 47: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [47]BBK P1 ModuleSession 1 & 2

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 48: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [48]BBK P1 ModuleSession 1 & 2

Example

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

function make_name($first,$last)

{

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

return $fullname;

}

Page 49: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [49]BBK P1 ModuleSession 1 & 2

Calling functions..

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

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

e.g.

echo make_name(‘Rob’,’Tuley’);// echoes ‘Rob Tuley’

Page 50: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [50]BBK P1 ModuleSession 1 & 2

Functions: Return Values

• Use return()– Causes execution of function to cease– Control returns to calling script

• To return multiple values– Return an array

• If no value returned– NULL

Page 51: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [51]BBK P1 ModuleSession 1 & 2

‘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 52: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [52]BBK P1 ModuleSession 1 & 2

In other words..• Variables within a function

– Are local to that function• Disappear when function execution ends

• Variables outside a function– Are not available within the function

• Unless set as global

• Remembering variables– Not stored between function calls

• Unless set as static

Page 53: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [53]BBK P1 ModuleSession 1 & 2

Global variables..• To access a variable outside the ‘local’ scope of

a function, declare it as a global:function add5toa(){global $a;$a = $a + 5;

} $a = 9;add5toa();echo $a; // 14

Page 54: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [54]BBK P1 ModuleSession 1 & 2

Static Variables• Local function variable values are not saved

between function calls unless they are declared as static:

function counter(){static $num = 0;return ++$num;

} echo counter(); // 1echo counter(); // 2echo counter(); // 3

Page 55: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [55]BBK P1 ModuleSession 1 & 2

Default Arguments

• Can specify a default value in the function definition which is used only if no value is passed to the function when called..

• Defaults must be specified last in the list

function myfunction($arg1,$arg2=‘blah’)…

function myfunction($arg1=‘blah’,$arg2)…

Page 56: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [56]BBK P1 ModuleSession 1 & 2

Passing References

• Pass a reference to a variable– Not the actual variable

• Why?– Enables a function to modify its arguments

• How?– Use an ampersand in front of the variable– &$variable

Page 57: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [57]BBK P1 ModuleSession 1 & 2

Hands On Exercise 5

Functions

Page 58: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On

5-May-2007 : [58]BBK P1 ModuleSession 1 & 2

Review

• More PHP!– Expressions– Operators– Control Structures– Functions