Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

ISBN:
0-13-145091-3
© 2004
pages: 1420

Order
Amazon logo

This tutorial presents four PHP programs that introduce PHP programming concepts, including comments, variables, operators, keywords, type conversions, scripting delimiters, interpolation, string concatenation, control statements and arrays. The techniques you learn hear are used in our subsequent tutorials:
[Note: This tutorial is an excerpt (Section 26.2) of Chapter 26, PHP, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.900-909. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
26.2 Introduction to PHP Programming (Continued)
[Figure 26.6] In addition to integer indices, arrays can have nonnumeric indices (lines 39-41). For example, indices ArtTic, LunaTic and GalAnt are assigned the values 21, 18 and 23, respectively. PHP provides functions for iterating through the elements of an array (lines 45-46). Each array has a built-in internal pointer, which points to the array element currently being referenced. Function reset sets the internal pointer to the first element of the array. Function key returns the index of the element currently referenced by the internal pointer, and function next moves the internal pointer to the next element. The for loop continues to execute as long as function key returns an index. Function next returns false when there are no additional elements in the array. When this occurs, function key cannot return an index, and the for loop terminates. Line 47 prints the index and value of each element.
Fig. 26.6 Array manipulation. (Part 1 of 3.)
1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3   
4   <!-- Fig. 26.6: arrays.php -->
5   <!-- Array manipulation -->
6   
7   <html xmlns = "http://www.w3.org/1999/xhtml">
8      <head>
9         <title>Array manipulation</title>
10       </head>
11   
12       <body>
13         <?php
14
15            // create array first
16            print( "<strong>Creating the first array</strong>
17               <br />" );
18            $first[ 0 ] = "zero";
19            $first[ 1 ] = "one";
20            $first[ 2 ] = "two";
21            $first[] = "three";
22   
23             // print each element's index and value
24            for ( $i = 0; $i < count( $first ); $i++ )
25               print( "Element $i is $first[$i] <br />" );
26   
27            print( "<br /><strong>Creating the second array
28               </strong><br />" );
29   
30             // call function array to create array second
31            $second = array( "zero", "one", "two", "three" );
32            for ( $i = 0; $i < count( $second ); $i++ )
33               print( "Element $i is $second[$i] <br />" );
34   
35            print( "<br /><strong>Creating the third array
36               </strong><br />" );
37   
38            // assign values to non-numerical indices
39            $third[ "ArtTic" ] = 21;
40            $third[ "LunaTic" ] = 18;
41            $third[ "GalAnt" ] = 23;
42   
43             // iterate through the array elements and print each
44             // element's name and value
45            for ( reset( $third ); $element = key( $third );
46               next( $third ) )
47               print( "$element is $third[$element] <br />" );
48   
49            print( "<br /><strong>Creating the fourth array
50               </strong><br />" );
51   
52             // call function array to create array fourth using
53             // string indices
54            $fourth = array(
55               "January" => "first", "February" => "second",
56               "March" => "third", "April" => "fourth",
57               "May" => "fifth", "June" => "sixth",
58               "July" => "seventh", "August" => "eighth",
59               "September" => "ninth", "October" => "tenth",
60               "November" => "eleventh","December" => "twelfth"
61               );
62   
63             // print each element's name and value
64            foreach ( $fourth as $element => $value )
65               print( "$element is the $value month <br />" );
66         ?>
67          </body>
68   </html>

  


Function array can also be used to initialize arrays with string indices. In order to override the automatic numeric indexing performed by function array, use operator =>, as demonstrated in lines 54-61. The value to the left of the operator is the array index, and the value to the right is the element's value. An array with string indices also is called an associative array.
The foreach loop (lines 64-65) is a control statement that is specially designed for iterating through arrays. The syntax for a foreach loop starts with the array to iterate through, followed by the keyword as, followed by the variables to receive the index and the value for each element. We use the foreach loop to print each element and value of array $fourth.
 
Page 1 | 2 | 3 | 4 | 5 | 6 | 7
 
Other PHP Tutorials
PHP Tutorial 1: Introduction to PHP
PHP Tutorial 2: Creating Simple PHP Programs (You are here)
PHP Tutorial 3: String Processing and Regular Expressions
PHP Tutorial 4: Form Processing and Business Logic