Main PDF 3 - PHP Arrays and User Defined Functions
Main PDF 3 - PHP Arrays and User Defined Functions
Emerging Technologies
MODULE 3
PHP Arrays and User Defined
Functions
PHP Arrays and User Defined Functions
Objectives
• To know the different approach of using arrays in PHP.
• To be familiar with using print_r function and var_dump
function for array inspection.
• To use PHP lazy function foreach to iterate through array
elements.
• To implement one dimensional and multi-dimensional in the
program.
• To know what is associative array in PHP.
• To use different predefined functions in manipulating array
elements.
• To defined what is function and to use function keyword in
declaring function in PHP.
• To create a user defined function using different approach.
PHP Arrays and User Defined Functions
Objectives (continue)
• To know use variables that is globally declared and locally
declared.
• To create static variables that can be used in accumulating
data every time the function calls.
PHP Arrays
PHP Arrays and User Defined Functions
Array
is used to aggregate a series of similar items together, arranging and
dereferencing them in some specific way.
print_r() function
short for print recursive. This takes an argument of any type
and prints it out, which includes printing all its parts
recursively.
var_dump() function
is same as the print_r function except that it prints additional
information about the size and type of the values it discovers
foreach() function
is a statement used to iterate or loop through the element in an
array. With each loop, a foreach statement moves to the next
element in an array.
foreach statement
specify an array expression within a set of parenthesis
following the foreach keyword.
Syntax
PHP Arrays and User Defined Functions
Example: Output:
PHP Arrays and User Defined Functions
Example:
Output:
PHP Arrays and User Defined Functions
Example:
PHP Arrays and User Defined Functions
Arrays: Sorting
Function Description
sort($array) Sorts by value; assign new numbers as the keys
rsort($array) Sorts by value in reverse order; assign new number
as the keys
asort($array) Sorts by value; keeps the same key
arsort($array) Sorts by value in reverse order; keeps the same key
ksort($array) Sorts by key
krsort($array) Sorts by key in reverse order
usort($array, Sorts by a function
functionname)
PHP Arrays and User Defined Functions
Arrays: Sorting
Example: Output:
PHP Arrays and User Defined Functions
Arrays: Sorting
Example: Output:
PHP Arrays and User Defined Functions
Arrays: Sorting
Example: Output:
PHP User Defined Functions
PHP Arrays and User Defined Functions
Functions
is a group of PHP statements that performs a specific task. Functions are
designed to allow you to reuse the same code in different locations.
Predefined functions
functions that are built-in into PHP to perform some standard operations
PHP Arrays and User Defined Functions
Syntax
function name(param){
//code to be executed by the function
}
Where
function – is the keyword used to declare a function
name – is the name of the function or function
identifier
param – is the formal parameters of the function.
Parameter must follow the rule of naming
dentifier.
PHP Arrays and User Defined Functions
Example: Output:
Slide 22
PHP Arrays and User Defined Functions
Example: Output:
PHP Arrays and User Defined Functions
Example: Output:
PHP Arrays and User Defined Functions
Example:
Output:
Example: Output:
PHP Arrays and User Defined Functions
Global Variables
is one that declared outside a function and is available to all
parts of the program.
Local Variables
is declared inside a function and is only available within the
function in which it is declared.
Static Variables
is used to retain the values calls to the same function.
PHP Arrays and User Defined Functions
Example: Output:
Example: Output:
PHP Arrays and User Defined Functions
Example: Output:
Example: Output:
PHP Arrays and User Defined Functions
Example:
Output:
An introduction to PHP web programming
Summary
• Array is used to aggregate a series of similar items together.
• Array index references a corresponding value.
• Array index can be simple numerical or have some direct
correlation to the value.
• Array index is also known as Array Keys.
• print_r function is used to print the array structure.
• var_dump function is same as print_r function except it adds
additional information about the data of each element.
• The foreach statement is use to iterate through the element in an
array.
• Using foreach statement you can display both the keys and value
of each element in the array.
• PHP provides functions for array manipulation such as sort(),
rsort(), asort(), arsort(), ksort(), krsort(), and usort() functions.
An introduction to PHP web programming
Summary (continue)
• sort(), asort(), and ksort() functions are used to sort elements in the
array in ascending order.
• rsort(), arsort(), and krsort() functions are used to sort elements in
the array in descending order.
• sort() and rsort() does not maintain its index reference for each
values.
• asort(), ksort(), arsort(), and krsort() maintains its reference for each
values.
• asort() and arsort() used to sort elements by values.
• ksort() and krsort() used to sort elements by keys.
• Functions is a group of PHP statements that performs a specific
task.
• Functions can be user defined generally defined by the user of the
program and predefined that are build in using libraries.
An introduction to PHP web programming
Summary (continue)
• You use functions in different ways. Function can only do
something without passing values. You can pass values to a
function and you can ask functions to return a value.
• function keyword is used in PHP to declare a function.
• A function that is declared inside a function is said to be hidden.
• To gain access to a variable that is outside from the function we
use the global keyword.
• We use static keyword to declare a variable inside a function that
will act as accumulator variable this will let the program remember
the last value of the variable that was used.