Page 148 - Beginning PHP 5.3
P. 148

Part II: Learning the Language
                  To manipulate the pointer and access the elements that it points to, use the following functions:


                  Function     Description

                  current()    Returns the value of the current element pointed to by the pointer, without
                               changing the pointer position.

                  key()        Returns the index of the current element pointed to by the pointer, without
                               changing the pointer position.
                  next()       Moves the pointer forward to the next element, and returns that element’s value.

                  prev()       Moves the pointer backward to the previous element, and returns that element’s value.
                  end()        Moves the pointer to the last element in the array, and returns that element’s value.
                  reset()      Moves the pointer to the first element in the array, and returns that element’s value.


                   Each of these functions takes just one argument  —  the array  —  and returns the required element ’ s value
                 or index, or   false  if an element couldn ’ t be found (for example, if you use  next()  when the pointer is
                at the end of the array, or you use   current()  on an empty array).


                  Here ’ s an example script that uses each of these functions. You can see the result in Figure  6-2 .
                      < !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                      “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
                      < html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
                       < head >
                         < title > Stepping Through an Array < /title >
                         < link rel=”stylesheet” type=”text/css” href=”common.css” / >
                       < /head >
                       < body >
                         < h1 > Stepping Through an Array < /h1 >

                      < ?php
                    $authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );

                    echo “ < p > The array: “ . print_r( $authors, true ) . “ < /p > ”;
                    echo “ < p > The current element is: “ . current( $authors ) . “. < /p > ”;
                    echo “ < p > The next element is: “ . next( $authors ) . “. < /p > ”;
                    echo “ < p > ...and its index is: “ . key( $authors ) . “. < /p > ”;
                    echo “ < p > The next element is: “ . next( $authors ) . “. < /p > ”;
                    echo “ < p > The previous element is: “ . prev( $authors ) . “. < /p > ”;
                    echo “ < p > The first element is: “ . reset( $authors ) . “. < /p > ”;
                    echo “ < p > The last element is: “ . end( $authors ) . “. < /p > ”;
                    echo “ < p > The previous element is: “ . prev( $authors ) . “. < /p > ”;

                    ? >
                       < /body >
                      < /html >
              110





                                                                                                      9/21/09   9:00:11 AM
          c06.indd   110                                                                              9/21/09   9:00:11 AM
          c06.indd   110
   143   144   145   146   147   148   149   150   151   152   153