1. Computing & Technology

Discuss in my forum

How to define an array in PHP

By , About.com Guide

In PHP you can give a single definition to a variable. If you wanted to add a list of items to a variable, you would be creating an array. Below is how you create an array in PHP.

<?php 
// define an array
$WoWClasses = array('Druid', 'Shaman', 'Paladin', 'Hunter', 'Warrior', 'Death Knight', 'Priest', 'Mage', 'Warlock');
print_r($WoWClasses);
?>

In our example above, we can then print the contents of the array using print_r. Once we know how to create an array, we can then use loops to cycle though them using all of their contents.

©2012 About.com. All rights reserved.

A part of The New York Times Company.