Home arrow PHP arrow Page 2 - PHP: The Switch Statement and Arrays

The Variable's Big Brother - PHP

In our last exciting adventure (back in early November), we braved crocodiles, ravenous editors, most of the PHP statements, and beginning loops. In this edition we'll cover the final statement, the Switch, and discuss arrays. So sit back, order your R2D2 robot to bring you a cold, frosty Jolt Cola, and let's get cracking.

TABLE OF CONTENTS:
  1. PHP: The Switch Statement and Arrays
  2. The Variable's Big Brother
  3. Printing the Array Values
  4. Associative Arrays
  5. The Multidimensional Array
  6. Beyond Multidimensional Arrays
By: James Payne
Rating: starstarstarstarstar / 6
January 07, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Yeah you heard me. You'd better not mess with the variable, or he'll get his big brother, the Array, to lay the goon hand down on you. And you don't want that, because he is bigger, badder, and holds a lot more data.

Arrays in PHP are similar to the arrays in most programming languages. Like a variable, they hold data. Unlike variables, they hold more than one piece of data. They can hold as many as you like, what with their giant, rippling muscles. There are three types of arrays, and we are going to discuss them all here. They are the Numeric Array, the Associative Array, and my favorite, the squeezably soft Multidimensional Array.

Numeric Arrays

The name of the Numeric Array is a little misleading. When I first read it I thought they only stored numbers, which of course is wrong. The Numeric part simply refers to how the values in an array are stored. Other languages were smart enough not to name their arrays Numeric; I don't know why PHP wasn't.

The easiest way to think of an array is like a drawer with many file folders in it. Each file within the drawer holds data. If that is confusing, then gaze at my marvelous code, which will enlighten your puny brain:


$mygirlfriends = array(“Your Mother”, “Angelina Jolie”, “Whitney White”);

In the above example, PHP automatically assigns the numeric index to your array. The value in the first position (Your Mother) gets the index of 0, the second position (Angelina Jolie) gets 1, and the third position (Whitney White) gets the index 2.

If we wanted to assign the index number manually, we could also write it this way:


$mygirlfriends[0] = “Your Mother”;

$mygirlfriends[1] = “Angelina Jolie”;

$mygirlfriends[2] = “Whitney White”;

Both ways store the information the same way; which is the best manner to assign the array values is up to you.



 
 
>>> More PHP Articles          >>> More By James Payne
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: