Although it might not necessarily make sense to learn how to output an array before even knowing how to create one in PHP, this function is so heavily used throughout this chapter, and indeed, throughout you daily programming life, that it merits first mention in this chapter. print_r() boolean print_r(mixed variable [, boolean return]) The print_r() function takes as input any variable and sends its contents to standard output, returning TRUE on success and FALSE otherwise. This in itself isn’t particularly exciting, until you take into account that it will organize array contents (as well as an object’s) into a very readable format before displaying them. For example, suppose you wanted to view the contents of an associative array consisting of states and their corresponding state capitals. You could call print_r() like this: print_r($states); This returns the following: Array ( [Ohio] => Columbus [Iowa] => Des Moines [Arizona] => Phoenix ) The optional parameter return modifies the function’s behavior, causing it to return the output to the caller, rather than sending it to standard output. Therefore, if you want to return the contents of the above $states array, you just set return to TRUE: $stateCapitals = print_r($states, TRUE); This function is used repeatedly throughout this chapter as a simple means for displaying the results of the example at hand.
Creating an Array Unlike other, much more annoying array implementations of other languages, PHP doesn’t require that you assign a size to an array at creation time. In fact, because it’s a loosely-typed language, PHP doesn’t even require that you declare the array before you use it. Despite the lack of restriction, PHP offers both a formalized and informal array declaration methodology. Each has its advantages, and both are worth learning. We’ll examine both in this section, starting with the informal variety. You reference the individual elements of a PHP array by denoting the element between a pair of square brackets. Because there is no size limitation on the array, you can create the array simply by making reference to it, like this: $state[0] = "Delaware"; You could then display the first element of the array $state like this: echo $state[0]; You can then add additional values by referencing the intended value in conjunction with the array index, like this: $state[1] = "Pennsylvania"; Interestingly, if you assume the index value is numerical and ascending, you can choose to omit the index value at creation time: $state[] = "Pennsylvania"; Creating associative arrays in this fashion is equally trivial, except that the associative index reference is always required. The following example creates an array that matches U.S. state names with their date of entry into the Union: $state["Delaware"] = "December 7, 1787"; Next I’ll discuss a functionally identical, yet somewhat more formal means for creating arrays: via the array() function. array() array array([item1 [,item2 ... [,itemN]]]) The array() function takes as its input zero or more items and returns an array consisting of these input elements. Here is an example of using array() to create an indexed array: $languages = array ("English", "Gaelic", "Spanish"); You can also use array() to create an associative array, like this: $languages = array ("Spain" => "Spanish", list() void list(mixed...) The list() function is similar to array(), though it’s used to make simultaneous variable assignments from values extracted from an array in just one operation. This construct can be particularly useful when you’re extracting information from a database or file. For example, suppose you wanted to format and output information read from a text file. Each line of the file contains user information, including name, occupation, and favorite color, with each piece of information delimited by a vertical bar ( | ). A typical line would look similar to the following: Nino Sanzi|Professional Golfer|green Using list(), a simple loop could read each line, assign each piece of data to a variable, and format and display the data as needed. Here’s how you could use list() to make multiple variable assignments simultaneously: // While the EOF hasn't been reached, get next line Each line would in turn be read and formatted similar to this: ==================================================== Reviewing the example, list() depends on the function explode()to split each line into three elements, which in turn uses the vertical bar as the element delimiter. The explode() function is formally introduced in Chapter 9. These elements are then assigned to $name, $occupation, and $color. At that point, it’s just a matter of format- ting for display to the browser. range() array range(int low, int high [,int step]) The range() function provides an easy way to quickly create and fill an array consisting of a range of low and high integer values. An array containing all integer values in this range is returned. As an example, suppose you need an array consisting of all possible face values of a die: $die = range(0,6); The optional step parameter offers a convenient means for determining the increment between members of the range. For example, if you want an array consisting of all even values between 0 and 20, you could use an step value of 2: $even = range(0,20,2); The range() function can also be used for character sequences. For example, suppose you wanted to create an array consisting of the letters A through F: $letters = range("A","F");
blog comments powered by Disqus |
|
|
|
|
|
|
|