The switch statement is wondrous and magic.

Topics: Array, C++, Florida Keys Pages: 28 (7287 words) Published: March 21, 2013
The switch statement is wondrous and magic. It's a piece of the language that allows you to select between different options for a value, and run different pieces of code depending on which value is set. Arrays

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible. . Syntax

An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments. array( key => value,
key2 => value2,
key3 => value3,

The keys were the numbers we specified in the array and the values were the names of the employees. Each key of an array represents a value that we can manipulate and reference. The general form for setting the key of an array equal to a value is: * $array[key] = value;

* |
| Programming PHPBy Kevin Tatroe, Rasmus Lerdorf
March 2002
1-56592-610-2, Order Number: 6102
524 pages, $39.95 US $61.95 CA |

Chapter 5
As we discussed in Chapter 2, PHP supports both scalar and compound data types. In this chapter, we'll discuss one of the compound types: arrays. An array is a collection of data values, organized as an ordered collection of key-value pairs. This chapter talks about creating an array, adding and removing elements from an array, and looping over the contents of an array. There are many built-in functions that work with arrays in PHP, because arrays are very common and useful. For example, if you want to send email to more than one email address, you'll store the email addresses in an array and then loop through the array, sending the message to the current email address. Also, if you have a form that permits multiple selections, the items the user selected are returned in an array. Indexed Versus Associative Arrays

There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value. PHP internally stores all arrays as associative arrays, so the only difference between associative and indexed arrays is what the keys happen to be. Some array features are provided mainly for use with indexed arrays, because they assume that you have or want keys that are consecutive integers beginning at 0. In both cases, the keys are unique--that is, you can't have two elements with the same key, regardless of whether the key is a string or an integer. PHP arrays have an internal order to their elements that is independent of the keys and values, and there are functions that you can use to traverse the arrays based on this internal order. The order is normally that in which values were inserted into the array, but the sorting functions described later let you change the order to one based on keys, values, or anything else you choose. Identifying Elements of an Array

You can access specific values from an array using the array variable's name, followed by the element's key (sometimes called the index) within square brackets: $age['Fred']
The key can be either a string or an integer. String values that are equivalent to integer numbers (without leading zeros) are treated as integers. Thus, $array[3] and $array['3'] reference the same element, but $array['03'] references a different element. Negative numbers are valid keys, and they don't specify positions from the end of the array as they do in Perl. You don't have to quote single-word strings. For instance, $age['Fred'] is the same as $age[Fred]. However, it's...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay on Switch Statement and Command
  • The Magic Research Paper
  • magic Essay
  • Magic Essay
  • statement of purpose Essay
  • Personal Statement Essay
  • statement Essay
  • Statement of Purpose Essay

Become a StudyMode Member

Sign Up - It's Free