Learn PHP – Loops and Arrays

September 10th, 2017

3.7 Arrays

Arrays are a simple, crude but useful way of storing data for short term use in a program, for insertion into or extraction from a table or SQL file on a server. Here are the examples from the book on p46 and p47.

Program

<?
$vital_stats = array(185, 155, "black", 42);
echo $vital_stats[0];
echo "</br>";
echo $vital_stats[1];
echo "</br>";
echo $vital_stats[2];
echo "</br>";
echo $vital_stats[3];
?>

Copy and past the code above into a file called array.php and save it in the htdocs folder of your Server2Go installation, refresh the browser page and run it by clicking on the file name.

Output

185
155
black
42


What the book fails to point out, but which becomes evident if you try to run their version of the program, is that the contents of an array start from 0 not 1, as you will see if you alter the numbers in square brackets by way of experiment.

Named Arrays

Program

<? $vital_stats = array("height" => 185, "weight" => 155,
"hair_colour" => "black", "shoe_size" => 42);
echo $vital_stats['height'];
echo "</br>";
echo $vital_stats['weight'];
echo "</br>";
echo $vital_stats['hair_colour'];
echo "</br>";
echo $vital_stats['shoe_size'];
?>

Copy and past the code above into a file called array-1.php and save it in the htdocs folder of your Server2Go installation, refresh the browser page and run it by clicking on the file name.

Output

185
155
black
42


Learn PHP – Loops

Syntax

for (start; condition; loop;) {

statements ;

}

Program

<?
for ($i=1; $i<=100; $i++) {
echo "I must not use PHP to write my lines.</BR>" ;
}
?>

Output

I must not use PHP to write my lines.
I must not use PHP to write my lines.
I must not use PHP to write my lines.

and so on …


While is another way to do it as below:-

Program

<?
$i = 1 ;
while ($i <= 100) { 
echo "I must not use PHP to write my lines.</BR>" ; 
$i++ ; 
}
?>

Output

I must not use PHP to write my lines.
I must not use PHP to write my lines.
I must not use PHP to write my lines.

and so on …

We’ll come back to While later when we want to read data from an array and write it into a table.


Foreach …

This is for use with arrays.

Syntax

foreach ($array as $value;) {

statements using $value ;

}

and

foreach ($array as $key → $value;) {

statements using $key and $value ;

}

Program

Using the earlier Array example above we might have:

<?
$vital_stats = array(185, 155, "black", 42);
echo "The vital statistics are:<P>" ;
foreach ($vital_stats as $stat) {
echo "$stat</BR>" ;
}
?>

Output

The vital statistics are:

185
155
black
42


Now to Foreach by key and value

Program

<?
$vital_stats = array("height" => 185, "weight" => 155,
"hair_colour" => "black", "shoe_size" => 42);
echo "The vital statistics are:<P>" ;
foreach ($vital_stats as $name => $stat) {
echo "$name ($stat)</BR>" ;
}
?>

The important thing to note is that unlike the earlier loop printing lines, that was limited by a number (100 lines) in this case the FOREACH function is not limited by an end stop number it stops printing lines because it runs out of data in the array. The significance of this will become clearer as we move on to inserting and printing data to and from MySQL files.

Leave a Reply