web database programming week 3
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 PresentationTRANSCRIPT
Web Database ProgrammingWeek 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– Do not reinvent the wheel
• User defined functions
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
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
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
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.
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
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]
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
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()
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()