web database programming week 3

11
Web Database Programming Week 3 PHP (2)

Upload: quon-hubbard

Post on 31-Dec-2015

23 views

Category:

Documents


0 download

DESCRIPTION

Web Database Programming Week 3. PHP (2). Functions. Group related statements together to serve “ a ” specific purpose Avoid duplicated code Easy maintenance of code No need to change multiple places Built-in functions E.g. print(), phpinfo() Use PHP manual to look up existing functions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Web Database Programming Week 3

Web Database ProgrammingWeek 3

PHP (2)

Page 2: Web Database Programming Week 3

Functions

• Group related statements together to serve “a” specific purpose– Avoid duplicated code– Easy maintenance of code

• No need to change multiple places

• Built-in functions– E.g. print(), phpinfo()– Use PHP manual to look up existing functions– Do not reinvent the wheel

• User defined functions

Page 3: Web Database Programming Week 3

Function Definition

• Define a function– “function” keyword– Function name– Parameters (input)– Function body (the real work)– Return statement (output)

• E.g. a function to make a string display in Bold

Page 4: Web Database Programming Week 3

Use a function

• Call the function– Invoke the function by use its name, and– Pass in the values for parameters– Must match the function definition– The value of a function is its return value

Page 5: Web Database Programming Week 3

Variable Scope

• Variables defined inside a function (including the parameters) are limited to use within the function only– They are undeclared (invisible) outside the function– A new variable with the same name will be created

if it appears outside the function body– They disappear when the function exits

• Variables defined outside a function is global, visible throughout the file

• Scopes are nested– Inner scope take precedence

Page 6: Web Database Programming Week 3

Exceptions

• Global statement indicates a variable is global– A global variable can be used inside a

function

• Static statement indicates a variable is static – Does not lose value after the function exits– E.g. a variable that count how many times

a function is called.

Page 7: Web Database Programming Week 3

Passing arguments into function

• Default is to pass by value– Only the value of a variable is passed in (copy),

the original variable does not change

• PHP support pass by reference– The reference (address) to the variable is passed

in, so the original variable can be changed by the function

– Prefix with an ampersand character “&”– PHP also support assignment by reference

• Default argument value– The argument can then be omitted in function call

Page 8: Web Database Programming Week 3

Array

• An ordered set of variables (elements)– Pairs of key => value – Allow random access by key– Key can be integers (default) or string– Value can be any type, even array

• Define array: array()• Access an array element: array_expression[key]

Page 9: Web Database Programming Week 3

More Array

• Using unset() to delete an array or an array element

• The order of array elements is by the order of them entering the array

• Multidimensional array– An element can itself be an array– Access each dimension with an extra []

• Using foreach(array_expression as $key => $value) to loop through array

Page 10: Web Database Programming Week 3

Some Array Functions

• Number of elements: count()• Instances of each value: array_count_values()

• Create array with values: array_fill(), range()

• Break up a string into an array: explode()• Search in array: in_array(), array_key_exists()

• Sort: sort()

Page 11: Web Database Programming Week 3

String Manipulations

• Nth character of a string: $aString{n}• Length: strlen()• Change case: strtolower(), strtoupper()

• Trimming whitespace: ltrim(), rtrim(), trim()

• Compare: strcmp()• Substring: substr()• Search in string: strpos(), strstr()• Replace substring: substr_replace()