PHP Arrays

This page teaches you about PHP arrays.

An array can hold many variables at the same time. The different variables are called "elements" and each element has a key and value. The syntax of an array is:

Example $array[key] = value;


Numerical Array

A numeric array uses numbers as the keys.

PHP Page

<html>
<head>
<title>HTML tutorial</title>
</head>

<body>

<?php
$cities[0] = "New York";
$cities[1] = "Chicago";
$cities[2] = "Miami";
$cities[3] = "Boston";

echo "I have lived in " . $cities[1] . "and" . $cities[2];
?>

</body>
</html>



This is how it looks

I have lived in Chicago and Miami.




Associative Array

An associative array uses strings as keys.

PHP Page

<html>
<head>
<title>HTML tutorial</title>
</head>

<body>

<?php
$birthyear[dave] = "1973";
$birthyear[brian] = "1958";
$birthyear[joe] = "1992";

echo "Joe was born in " . $birthyear[joe] . ".";
?>

</body>
</html>



This is how it looks

Joe was born in 1992.





 
Bookmark this page: