Professional Documents
Culture Documents
Week3 Array
Week3 Array
PHP Array
2020
• Arrays are special data types. Despite of other normal variables an array can
store more than one value. It contains set of data represented by a single
name.
• These are lists of information mapped with keys and stored under one
variable name
Syntax:
$array_name = array (values);
3
Array in PHP can be written as
2020
or
$color[] = “red”;
$color[] = “blue”;
$color[] = “green”;
$color[] = “yellow”;
$color[] = “black”;
4
Example
2020
$num = 7;
$foo = array(“this is an element”,5,$num);
Index
5
with loop
2020
6
Associative Array
2020
7
2020
foreach(arrayVar as value)
{
code to be executed;
}
foreach(arrayVar as key => value)
{
code to be executed;
}
10
2020
<html>
<head><title> Using Array</title></head>
<?php
$scientist = array(‘Last Name’ => ‘Einstein’,
‘First Name’ => ‘Albert’,
‘Age’ => 129);
echo “<h3>Scientist Information</h3>”;
foreach($scientist as $inventor) {
echo “$inventor <br/>”;
}
?>
</html>
11
2020
12
2020
13
2020
$scientist = array
(‘PHYSICS’ => array(‘lname’ => ‘Einstein’,
‘fname’ => ‘Albert’,
$field
‘age’ => 129), $inventor[
‘ANATOMY’ => array(‘lname’ => ‘Da Vinci’, ]
‘fname’ => ‘Leonardo’,
‘age’ => 130));
14
Sample Output:
2020
15
Sorting Arrays
2020
A common task you may find yourself doing with arrays is sorting
their values. PHP provides many functions that making sorting array
values easy. Here are just a few common array - sorting functions:
Array-Sorting Description
Function
sort($array) Sorts an array in ascending value order
rsort($array) Sorts an array in descending value order
asort($array) Sorts an array in ascending value order while
maintaining the key/value relationship
arsort($array) Sorts an array in descending value order while
maintaining the
16
Example
2020
17
Example 2020
18
2020
19
User-Defined Functions
2020
Syntax:
<?php
function name(parameters) {
code to be executed;
}
?>
20
Example
2020
<?php
function writeName()
{
echo “Dr. Jose P. Rizal”;
}
echo "My name is ";
writeName();
?>
21
Parameters
2020
22
2020
<?php
function writeName($fname)
{
echo $fname . " Rizal.<br />";
}
23
Output:
2020
24
Returning Values
2020
<?php
function add($x=1, $y=16){
$total= $x + $y;
return $total;
}
echo “1 + 16 = " . add();
?>
26
Nested Functions
2020
<?php
function add($x=1, $y=16){
$total= $x + $y;
return $total;
}
echo “1 + 16 = " . add();
?>
27
Example
2020
function fname(){
echo "fname<br>";
function lname() {
echo "lname<br>";
}
}
output:
fname();
lname();
28
Understanding Variable Scope
2020
<?php
$GlobVar = "Global Variable";
function scopeSamp() {
echo $GlobVar;
$LocalVar = "Local Variable
<br/>";
echo $LocalVar;
}
scopeSamp();
echo "$GlobVar <br/>";
echo "$LocalVar <br/>";
?>
30
The global Keyword
2020
31
Example
2020
$num_of_calls = 0;
function numberedHeading($txt) {
global $num_of_calls;
$num_of_calls++;
echo "<h1>$num_of_calls. $txt</h1>";
}
numberedHeading("Widgets");
echo “<p> We build a fine range of widgets.</p>”;
numberedHeading("Doodads");
echo “<p>Finest in the world.</p>”;
32
Output
2020
33
The static Statement
2020
34
Example
2020
function Tables(){
static $count=0;
static $x=0;
$y=2;
$count++;
$x += $y;
echo "$x <br/>";
if($count < 5)
{
Tables();
}
}
Tables();
35
Ouput
2020
36
2020
Questions??