Professional Documents
Culture Documents
ARRAY
- can store one or more values in a single variable name.
elements
- are the values that are stored in the array.
- each element in the array is referenced by an index.
index
- identifies the element by any other unique element in the array.
- the index value can be a number or a string, but it must be unique.
When working with PHP, sooner or later, you might want to create many similar variables.
Instead of having many similar variables, you can store the data as elements in an array.
Each element in the array has its own ID so that it can be easily accessed.
Example 1
$names = array("JM","Charles","Jorvel");
Example 2
$names = array();
$names[0] = "JM";
$names[1] = "Charles";
$names[2] = "Jorvel";
<?php
$names[0] = "JM";
$names[1] = "Charles";
$names[2] = "Jorvel";
1
DCIT111 – ADVANCED PROGRAMMING hayperaktib
Lecture # 8[Loops and Array]
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
With associative arrays we can use the values as keys and assign values to them.
Example 1
In this example we use an array to assign ages to the different persons:
Example 2
This example is the same as example 1, but shows a different way of creating the array:
$ages = array();
$ages['JM'] = 20;
$ages['Charles'] = 20;
$ages['Jorvel'] = 19;
<?php
$ages['JM'] = "20";
$ages['Charles'] = "20";
$ages['Jorvel'] = "19";
echo "JM is " . $ages['JM'] . " years old.";
?>
JM is 20 years old.
2
DCIT111 – ADVANCED PROGRAMMING hayperaktib
Lecture # 8[Loops and Array]
Multidimensional Arrays - each element in the main array can also be an array. And
each element in the sub-array can be an array, and so on.
Example
In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
(
"Jamison"=>array
(
"Peter",
"JM",
"Cindy"
),
"Merlan"=>array
(
"Charles"
),
"Penus"=>array
(
"Jorvel",
"Arnel",
"Jane"
)
);
Example 2
3
DCIT111 – ADVANCED PROGRAMMING hayperaktib
Lecture # 8[Loops and Array]
LOOP STRUCTURES
Programmers use loop or iteration statements to control sequences of statements
that are repeated according to runtime conditions.
Three types of iteration statements
o for
o while
o do-while
For Loop
For loops provide the same general functionality as while loops, but also provide for a
predefined location for initializing and changing a counter value. Their syntax is:
Syntax:
for(initialization expression; test expression; modification expression){
code that is executed;
}
This produces:
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Number is 10
When your PHP program process the for loop, the initialization portion is evaluated. For
each iteration of the portion that increments, the counter executes, followed by a check to
see whether you're done. The result is a much more compact and easy-to-read statement.
4
DCIT111 – ADVANCED PROGRAMMING hayperaktib
Lecture # 8[Loops and Array]
Other Examples Using the for Structure
for($i=1;$i<=100;$i++)
for($i=100;$i>=1;$i--)
for($i=7;$i<=77;$i+=7)
for($i=20;$i>=2;$i-=2)
for($x=2;$x<=20;$x+=3)
for($x=99;$x>=0;$x-=11)
while-loop
The while loop takes the expression followed by the code to execute.
Syntax:
initialization
while(condition){
statements if true;
body update
}
Figure B. How a
while loop executes
<?php
$num = 1;
print 'Done.';
?>
Output:
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Number is 10
Done.
5
DCIT111 – ADVANCED PROGRAMMING hayperaktib
Lecture # 8[Loops and Array]
Other While loop examples
Example 2: Example 3:
$ctr = 1; $counter = 1;
while($ctr<=10){ while($counter*$counter<1000){
echo $ctr + “ ”; echo $counter * $counter;
$ctr+=1; $counter++;
} }
Do-while loop
The do . . . while loop takes an expression such as a while statement but places it at the
end. The syntax is:
Syntax:
initialization
do {
statements if true;
body update;
} while(condition);
Example 2
<?php
$num = 1;
do {
echo "Number is ".$num."<br />";
$num++;
} while ($num <= 10);
echo "Done.";
?>