Page 85 - Beginning PHP 5.3
P. 85

Chapter 3: PHP Language Basics
                           In fact, there is one other string operator  —  the combined assignment operator  .=   —  which was
                         mentioned earlier in the chapter. It ’ s useful when you want to join a new string onto the end of an
                         existing string variable. For example, the following two lines of code both do the same thing  —  they
                         change the string variable   $x  by adding the string variable  $y  to the end of it:

                             $x = $x . $y;

                             $x .= $y;


                           Understanding Operator Precedence
                           With simple expressions, such as  3 + 4 , it ’ s clear what needs to be done (in this case,  “ add 3 and 4 to
                          produce 7 ” ). Once you start using more than one operator in an expression, however, things aren ’ t so
                          clear - cut. Consider the following example:

                             3 + 4 * 5

                           Is PHP supposed to add 3 to 4 to produce 7, then multiply the result by 5 to produce a final figure of 35?
                         Or should it multiply 4 by 5 first to get 20, then add 3 to make 23?

                           This is where operator precedence comes into play. All PHP operators are ordered according to
                         precedence. An operator with a higher precedence is executed before an operator with lower precedence.
                         In the case of the example,   *  has a higher precedence than  + , so PHP multiplies 4 by 5 first, then adds 3
                          to the result to get 23.

                           Here ’ s a list of all the operators you ’ ve encountered so far, in order of precedence (highest first):


                                          Precedence of Some PHP Operators (Highest First)
                                        ++  - - (increment/decrement)


                                        (int) (float) (string) (array) (object) (bool) (casting)
                                        ! (not)
                                        * / % (arithmetic)

                                        +  -  . (arithmetic)
                                         <    < =  >    > =  < > (comparison)




                                        == != === !== (comparison)
                                          & & (and)


                                        || (or)
                                        = +=  - = *= /= .= %= (assignment)
                                        and
                                        xor

                                        or


                                                                                                          47





                                                                                                      9/21/09   8:51:26 AM
          c03.indd   47
          c03.indd   47                                                                               9/21/09   8:51:26 AM
   80   81   82   83   84   85   86   87   88   89   90