Similar presentations

2
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] Reminder PHP is embedded within xhtml pages within the tags: The short version of these tags can also be used: Each line of PHP is terminated, like MySQL, with a semi-colon.

10
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] Comparison Operators ExampleNameResult $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.

13
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] Logical Operators ExampleNameResult $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.

17
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] 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..

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

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

25
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] 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!

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

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

40
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] 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!

43
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] 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..

45
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] 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..

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

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

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

55
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] 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)…