You are on page 1of 6

DCIT111 – ADVANCED PROGRAMMING hayperaktib

Lecture # 8[Loops and Array]

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.

There are three different kinds of arrays:

 Numeric array - An array with a numeric ID key


 Associative array - An array where each ID key is associated with a value
 Multidimensional array - An array containing one or more arrays

Numeric Arrays - stores each element with a numeric ID key.

There are different ways to create a numeric array.

Example 1

In this example the ID key is automatically assigned:

$names = array("JM","Charles","Jorvel");

Example 2

In this example we assign the ID key manually:

$names = array();
$names[0] = "JM";
$names[1] = "Charles";
$names[2] = "Jorvel";

The ID keys can be used in a script:

<?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";
?>

The code above will output:

Charles and Jorvel are JM's neighbors

Associative Arrays - each ID key is associated with a value.


When storing data about specific named values, a numerical array is not always the best
way to do it.

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:

$ages = array("JM"=>20, "Charles"=>20, "Jorvel"=>19);

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;

The ID keys can be used in a script:

<?php
$ages['JM'] = "20";
$ages['Charles'] = "20";
$ages['Jorvel'] = "19";
echo "JM is " . $ages['JM'] . " years old.";
?>

The code above will output:

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

Lets try displaying a single value from the array above:

echo "Is " . $families['Jamison'][2] . " a part of the Jamison family?";

The code above will output:

Is Cindy a part of the Jamison family?

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;
}

Figure A. How a for loop


executes
An example for loop is:
<?php
for ($num = 1; $num <= 10; $num++) {
print "Number is $num<br />\n";
}
?>

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

Example 1. A sample while loop that


counts to 10

<?php
$num = 1;

while ($num <= 10){


print "Number is $num<br />\n";
$num++;
}

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);

The do-while loop is similar in structure to a while-loop, except in its upside-down


appearance, as the condition appears after the body. As a consequence of the placement of
condition, the body of a do-while loop will be executed at least once, i.e., the minimum
number of iterations for a do-while loop is one. The maximum number of iterations is
infinity.
Do-While Examples
Example 1
$x=1;
do{
echo $x * $x * $x;
$x +=5;
} while($x<=100);

Example 2
<?php

$num = 1;

do {
echo "Number is ".$num."<br />";
$num++;
} while ($num <= 10);

echo "Done.";

?>

You might also like