Page 74 - Beginning PHP 5.3
P. 74

Part II: Learning the Language
                   As well as the four scalar types, PHP supports two compound types.  Compound  data is data that can
                 contain more than one value. The following table describes PHP ’ s compound types:



                       Compound Data Type        Description
                        Array                 An ordered map (contains names or numbers mapped to values)

                        Object                A type that may contain properties and methods

                   You look at arrays in Chapter  6 , and objects in Chapter  8 .

                   Finally, PHP supports two  special  data types, so called because they don ’ t contain scalar or compound
                 data as such, but have a specific meaning:


                      Special Data Type        Description
                     Resource             Contains a reference to an external resource, such as a file or database

                     Null               May only contain   null  as a value, meaning the variable explicitly
                                      does not contain any value


                  About Loose Typing

                   PHP is known as a  loosely - typed  language. This means that it ’ s not particularly fussy about the type of
                 data stored in a variable. It converts a variable ’ s data type automatically, depending on the context in
                 which the variable is used. For example, you can initialize a variable with an integer value; add a float
                 value to it, thereby turning it into a float; then join it onto a string value to produce a longer string.
                 In contrast, many other languages, such as Java, are  strongly - typed ; once you set the type of a variable in
                 Java, it must always contain data of that type.

                   PHP ’ s loose typing is both good and bad. On the plus side, it makes variables very flexible; the same
                 variable can easily be used in different situations. It also means that you don ’ t need to worry about
                 specifying the type of a variable when you declare it. However, PHP won ’ t tell you if you accidentally
                 pass around data of the wrong type. For example, PHP will happily let you pass a floating - point value to
                 a piece of code that expects to be working on an   integer  value. You probably won ’ t see an error
                 message, but you may discover that the output of your script isn ’ t quite what you expected! These types
                 of errors can be hard to track down. (Fortunately, there is a way to test the type of a variable, as you see
                 in a moment.)


                  Testing the Type of a Variable
                   You can determine the type of a variable at any time by using PHP ’ s  gettype()  function. To use
                  gettype() , pass in the variable whose type you want to test. The function then returns the variable ’ s
                 type as a string.





              36





                                                                                                      9/21/09   8:51:21 AM
          c03.indd   36                                                                               9/21/09   8:51:21 AM
          c03.indd   36
   69   70   71   72   73   74   75   76   77   78   79