Subtopic 1: PHP ARRAYS Arrays - Is Used To Aggregate A Series of Similar Items Together, Arranging and Dereferencing Them in
Subtopic 1: PHP ARRAYS Arrays - Is Used To Aggregate A Series of Similar Items Together, Arranging and Dereferencing Them in
Subtopic 1: PHP ARRAYS Arrays - Is Used To Aggregate A Series of Similar Items Together, Arranging and Dereferencing Them in
Arrays – 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 called as array keys.
Array can be used as ordinary same as in C and C++ arrays.
Example: Code 1
Example: Code 2
Example: Code 3
Array: Function 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 these
functions is to help you viualize what’s going on with compound data structures like arrays.
foreach() function – is a statement used t 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
Example:
Example:
Arrays: Sorting
Funstion 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
assort($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, functionname) Sorts by function
Example: Output:
Example: Output:
Syntax:
function name(param) {
//code to be executed nu the function
}
Where
function – is the keyword used to declare a function
name – is the name of he functions or function identifier
param – is the formal parameters of the function. Parameter must follow the rule of naming
identifier.
Example: Output:
Example: Output:
Example:
Example: Output:
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.
Example: Output:
Example: Output:
Example: Output:
Example:
SUBTOPIC 1: 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 you can display both the keys and value of each element in the array.
PHP provided function for array manipulation such as sort(), rsort(), asort(). Arsort(), ksort(), and
usort() functions.
sort(), asort() and ksort() functions are used to sort elements in the array in ascending order.
rsort(), arsort(), and ksort() functions are used to sort elements in the array in ascending order.
sort() and rsort() does not maintain its index reference for each values.
asort(), ksort(), arsort(), and krsort() maintains its reference for each values.
asrot() and arsort() used to sort elements by values.
ksort() and krsort() used to sort elements by keys.
SUBTOPIC 2: SUMMARY
CODIO
PHP ARRAY
Is used to aggregate a series of similar items together, arranging and dereferencing them in
some specific way.
An array stores multiple value in one single variable.
PHP array does not need to declare how many elements that the array variable have.
WHAT IS AN ARRAY?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables
could look like this:
$cars1 = “Volvo”;
$cars2 = “BMW”;
$cars3 = “Toyota”;
However, what if you want to loop through the cars and find a specific one? And what if you had
not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access the values by referring
to an index number.
ARRAY FUNCTIONS
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.
$fruit = array(“Apple”, “Banana”, “Grapes”, “Orange”);
print_r($fruit);
ARRAY ELEMENTS
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age= array(“Peter” =>”35”, “Ben” =>”37”, “Joe”=>”43”);
Or:
$age[‘Peter’] = “35”;
$age[‘Ben’] = “37”;
$age[‘Joe’] = “43”;
The named keys cab then be used in a script:
Example:
$age = array(“Peter”=>”35”, “Ben”=> ”37”, “Joe”=> “43”);
echo “Peter is “ .$age[‘Pete’] . “years old.”;
o loop through and print all the values of an associative array, you could use a foreach loop, like
this:
Example:
$age = array(“Peter”=> “35”, “Ben”=> “37”, “Joe”=> “43”);
Example:
MODULE 4
Using Constant
define() functions – used to declare constants. A constant can only be assigned a scalar value,
like a string or a number. A constant’s value cannot be changed.
Syntax:
define (‘NAME’, ‘value’);
Example: Output:
Including Files
You can separate your PHP file and embed it to your html by using PHP include functions.
Example:
Output:
Including Files: include() function (file not found)
Example:
Output:
Example:
Output:
Example: header.inc
Output:
Mathematical Function:
rand() function
- used to generate random integers
- syntax
int rand(void)
int rand(int #min, int $max)
ceil() function
- returns the next highest integer by rounding the value upwards.
- Syntax
float ceil(float $value)
floor() function
- returns the next lowest integer by rounding the value downwards.
- Syntax
float floor(float $value)
min() function
- return the smallest value
- syntax
mixed min(array $values)
mixed min(mixed $values1, mixed $values2[,mixed $...])
max() function
- return the highest value
- syntax
mixed max(array $values)
mixed max(mixed $values1, mixed $values2[,mixed $...])
Mathematical Function:
Example:
number_format() function
- Format a number with grouped thousand
- Syntax
string number_format (
float $number
[, int $decimals = 0 ] )
string number_format (
float $number ,
int $decimals = 0 ,
string $dec_point = '.' ,
string $thousands_sep = ',' )
Example: Output:
Subtopic 2: ARRAY, STRING AND DATE MANIPULATION
unset function
- destroys the specified variable
- syntax:
void unset ( mixed $var [, mixed $... ] )
explode function
- split a string by string
- syntax
array explode ( string $delimiter ,
string $string [, int $ limit ] )
implode function
- join array elements to form a string
- syntax
string implode ( string $glue , array $pieces )
string implode ( array $pieces )
Explain: Output:
strlen function
- return the value length of a string
- syntax
int strlen (string $string)
strops function
- find the position of the first occurrence of a substring in a given string
- syntax
int strpos ( string $haystack ,
mixed $needle [, int $offset = 0 ] )
atrrev function
- reverse a given string
- syntax
string strrev ( string $string )
strtolower function
- converts string to lowercase
- syntax
string strtolower ( string $str )
strtoupper function
- converts string to uppercase
- syntax
string strtoupper ( string $str )
substr function
- returns part of a given string
- syntax
string substr ( string $string ,
int $start [, int $length ] )
Example:
Output:
ucfirst() function
- Make a string’s first character uppercase
- Syntax
string ucfirst( string $str )
Ucwords() function
- converts the first character of each word in a string to uppercase
- syntax
string ucwords ( string $str )
trim() function
- stripped white spaces or other characters from the beginning and end of a string.
- Syntax
string trim ( string $str [, string $charlist ] )
ltrim() function
- strip white spaces or other characters from the beginning of a string.
- Syntax
string ltrim ( string $str [, string $charlist ] )
rtrim() function
- strip white spaces or other characters from the end of a string.
- Syntax
string rtrim ( string $str [, string $charlist ] )
strip_tags() function
- strip HTML and PHP tags from a string.
- Syntax
string strip_tags ( string $str [, string
$allowable_tags ] )
Example: Output:
get the unix timestamp (January 1, 1970) for a given date. (with strict notice)
same as time() function (without strict notice)
syntax:
int mktime ([ int $hour = date("H")
[, int $minute = date("i")
[, int $second = date("s")
[, int $month = date("n")
[, int $day = date("j")
[, int $year = date("Y")
[, int $is_dst = -1 ]]]]]]] )