Page 115 - Open Soource Technologies 304.indd
P. 115

Open Source Technologies



                   Notes         Here’s an example for the use of static that runs initialization code the  first time (and only the
                                 first time) the function is run:
                                       Example:       function do _something()
                                                           {
                                                                      static first _time = true;

                                                                      if (first _time) {
                                                                          // Execute this code only the first time the
                                                           function is

                                                                             →called
                                                                           ...
                                                                       }
                                                                     // Execute the function’s main logic every time
                                                       the function is
                                                                       →called
                                                                        ...

                                                                          }
                                 7.8 Arrays


                                 A variable is a storage area holding a number or text. The problem is, a variable will hold only
                                 one value. An array is a special variable, which can store multiple values in one single variable.
                                 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.

                                 For now, let’s take a look at the general syntax of the array() statement:

                                 Array array([key]=>[value], [index2]=>[value], ...);
                                 In PHP, there are three kinds of arrays:

                                    •  Numeric array - An array with a numeric index.
                                    •  Associative array - An array where each ID key is associated with a value.
                                    •  Multidimensional array - An array containing one or more arrays.

                                 Numeric Arrays
                                 A numeric array stores each array element with a numeric index. There are two methods to
                                 create a numeric array.

                                    1.  In the following example the index are automatically assigned (the index starts at 0):
                                      $cars=array(“Saab”,”Volvo”,”BMW”,”Toyota”);
                                   2.  In the following example we assign the index manually:

                                      $cars[0]=”Saab”;



        110                               LOVELY PROFESSIONAL UNIVERSITY
   110   111   112   113   114   115   116   117   118   119   120