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:
   
Rate this page:
  • Currently 2.63/5
  • 1
  • 2
  • 3
  • 4
  • 5

Rating: 2.6/5 (92 votes)



Comments:

please post comments
admin November 21, 2006

 
Nickname:
Comment:
Security: 1+1=
Hosts
Lunarpages.com Web Hosting
iPowerWeb

Domain Registrars
PPC Networks
Logos
Affiliate Networks
Resources
Hosts
Lunarpages.com Web Hosting
iPowerWeb

Domain Registrars
Namecheap
GoDaddy
NetworkSolutions
Register.com

PPC Networks
Yahoo Search
Miva
Enhance

Logos
LogoWorks
LogoJeez

Affiliate Networks
Azoogle
LinkShare

Resources
VBulletin