Posts Tagged ‘ arrays ’

Why use an array?

Recently, I asked myself the same question.  You might be wondering, what is an array.  According to Wiki:

In computer science an array [1] is a data structure consisting of a group of elements that are accessed by indexing.

What does that  really mean?  Well, I’ll explain by using an example I got off one of the programming forums I frequent, SamponVideos.com. I will actually quote some of Johnathan Sampson’s message because it captures the idea.  In his example, if you were creating a an image uploader, and you did not want to allow executable files, you would do something like this:

$fileTypes = array("jpeg","gif","jpg","bmp");

Without the use of an array, it would turn into this:

if ($fileExtension == “jpeg”) {
# Approved
} else
if (
$fileExtension == “gif”) {
# Approved
} else
if (
fileExtension == “jpg”) {
# Approved
}

Or this:

if ($fileExtension == “jpeg” || $fileExtension == “gif” || $fileExtension == “jpg”) {
# Approved
}

The array makes the code, much easier to handle, and looks cleaner.  Have you used arrays in your projects?

Category: PHP Script | Comments Off on Why use an array?

|

Flickr Photos

Quick Contact

Your Name (required)

Your Email

Your Message

captcha