If
you think about writing code without arrays, every value
would need to be stored using a simple variable. Each
item in the shopping list would need to be assigned to
a string variable using code like this
$item1
= "Bread";
$item2 = "Lemonade";
$item3 = "Apples";
$item4 = "Grapes";
$item5 = "Coffee";
which
is perfectly good PHP code but printing the entire list
would necessitate knowing - and coding - every variable
name, for instance
print("<BR>$item1<BR>$item2<BR>$item3<BR>$item4<BR>$item5");
which,
again, is perfectly good PHP code. But what happens when
you add Eggs to the list? If Eggs goes into
$item6 you'll need to change all the code that handles
the shopping list, adding an assignment for $item6
$item6
= "Eggs";
and
changing the print function to add a further HTML line
break (<BR>) and the additional variable, $item6.
If,
on the other hand, the shopping list was stored in
an array, the list could hold one or more entries. The
entries
could be accessed by their index and we could use the
following construct
$shoppinglist
= array("Bread", "Lemonade",
"Apples", "Grapes", "Coffee", "Eggs");
then
code to implement this |