0% found this document useful (0 votes)
4 views

Main PDF 3 - PHP Arrays and User Defined Functions

This document covers PHP arrays and user-defined functions, detailing their usage, types, and manipulation techniques. It explains how to visualize arrays using print_r and var_dump functions, iterate through them with foreach, and sort them using various sorting functions. Additionally, it discusses the concept of functions in PHP, including user-defined and predefined functions, variable scope, and the use of static variables.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Main PDF 3 - PHP Arrays and User Defined Functions

This document covers PHP arrays and user-defined functions, detailing their usage, types, and manipulation techniques. It explains how to visualize arrays using print_r and var_dump functions, iterate through them with foreach, and sort them using various sorting functions. Additionally, it discusses the concept of functions in PHP, including user-defined and predefined functions, variable scope, and the use of static variables.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Applications Development and

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.

• Each member of the array index references a corresponding


value and can be a simple numerical reference to the value’s
position in the series, or it could have some direct correlation to
the value.
• PHP array does not need to declare how many elements that
the array variable have.
• Array index in PHP can be also called as array keys.
• Array can be used as ordinary array same as in C and C++
arrays.
PHP Arrays and User Defined Functions

Array: one dimensional

Example: Code 1 Example: Code 2

Example: Code 3 Output:


PHP Arrays and User Defined Functions

Array: Functions for visualizing arrays

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

print_r() and var_dump() functions are commonly used


for debugging. The point of this of these functions is to help
you visualize what’s going on with compound data structures
like arrays.
PHP Arrays and User Defined Functions

Array: Functions for visualizing arrays

Example: using print_r() Output:

Example: using var_dump() Output:


PHP Arrays and User Defined Functions

Array: Looping through array elements

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

Array: Looping through array elements

$arr The name of the array that you’re walking through.


$key The name of the variable where you want to store
the key. (optional)
$value The name of the variable where you want to store
the value

Example: Output:
PHP Arrays and User Defined Functions

Array: Looping through array elements

Example:

Output:
PHP Arrays and User Defined Functions

Arrays: Multidimensional arrays Output:

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.

User defined functions


functions that are provided by the user of the program.

Predefined functions
functions that are built-in into PHP to perform some standard operations
PHP Arrays and User Defined Functions

Functions: 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

Functions: Function with no parameters

Example: Output:
Slide 22
PHP Arrays and User Defined Functions

Functions: Function with parameters

Example: Output:
PHP Arrays and User Defined Functions

Functions: Function that returns a value

Example: Output:
PHP Arrays and User Defined Functions

Functions: Nested function

Example:

Output:

Example: Output:
PHP Arrays and User Defined Functions

Functions: Variable scope

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

Functions: using variables

Example: Output:

Example: Output:
PHP Arrays and User Defined Functions

Functions: using variables

Example: Output:

Example: Output:
PHP Arrays and User Defined Functions

Functions: using variables

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.

You might also like