Array Variables


Arrays.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>arrays.php</title>
</head>

<body>
<p>My eight Web 2 students have just taken their final exam.  
It was very difficult so I decided to buy my 'A' students a new car. 
Here are their scores: </p> 

Student1: 89.4<br>
Student2: 89<br>
Student3: 90<br>
Student4: 91<br>
Student5: 89.3<br>
Student6: 92<br>
Student7: 99<br>
Student8 :99.9<br>


<p>Let's use PHP to display the great news.  
We will use an array to store the values.</p>
<?php
//This is the array to hold the grades
//notice that arrays are usually numbered 
//begining with zero.  This is called a zero index.
$grades[0]= 89.4;
$grades[1]= 89;
$grades[2]= 90;
$grades[3]= 91;
$grades[4]= 89.3;
$grades[5]= 92;
$grades[6]= 99;
$grades[7]= 99.9;

//Let's display the number of grades in the array
//by using the echo command and the count function.
//count() returns the number of items within the parenthesis
echo "<p>";
echo 
"There are ".count($grades)." grades in the array.";
echo 
"</p>";

//Here are the statements to test 
//whether students scored greater than 89.5 (I round up).
//Let's get fancy and use a for loop

//counter variable: Set the counter variable to zero 
//condition:run the code while the counter variable is less than 8
//REMEMBER there are 8 students, but we indexed at zero!
//counter increment: increase the counter on every loop

for ($counter=0$counter count($grades) ; $counter=$counter+1)
{
    if (
$grades[$counter] >89.5)
    {
    
//must add one to counter because index starts at zero
    
print("A new car for student ".($counter+1)."!<br>");
    }
}
?>

</body>
</html>