Page 84 - Beginning PHP 5.3
P. 84

Part II: Learning the Language
                   Now that you know how Boolean values work you can start combining Boolean values with logical
                 operators. PHP features six logical operators, and they all work in combination with   true  or  false
                Boolean values to produce a result of either   true  or  false :

                      Operator        Example        Result

                         & &   (and)        $x  & &  $y        true  if both  $x  and  $y  evaluate to  true ;  false  otherwise


                       and           $x and $y         true  if both  $x  and  $y  evaluate to  true ;  false  otherwise
                       ||  (or)        $x || $y        true  if either  $x  or  $y  evaluates to  true ;  false  otherwise

                       or            $x or $y        true  if either  $x  or  $y  evaluates to  true ;  false  otherwise
                       xor           $x xor $y         true  if  $x  or  $y  (but not both) evaluates to  true ;  false
                                                 otherwise
                       !  (not)       !$x           true  if  $x  is  false ;  false  if  $x  is  true


                  Here are some simple examples of logical operators in action:
                    $x = 2;
                    $y = 3;
                    echo ( ($x  >  1)  & &  ($x  <  5) ) .  “ < br / > ”;    // Displays 1 (true)


                    echo ( ($x == 2) or ($y == 0) ) . “ < br / > ”;  // Displays 1 (true)
                    echo ( ($x == 2) xor ($y == 3) ) . “ < br / > ”; // Displays “” (false) because both
                                                                 // expressions are true
                    echo ( !($x == 5 ) ) . “ < br / > ”;             // Displays 1 (true) because
                                                                 // $x does not equal 5
                  The main use of logical operators and Boolean logic is when making decisions and creating loops, which
                you explore in Chapter  4 .

                  You ’ re probably wondering why the   and  and  or  operators can also be written as   &  &   and  || . The reason is
                that   and  and  or  have a different precedence to   &  &   and  || . Operator precedence is explained in a moment.
                  String Operators
                   There ’ s really only one string operator, and that ’ s the  concatenation operator ,  .  (dot). This operator simply
                takes two string values, and joins the right - hand string onto the left - hand one to make a longer string.

                 For example:
                    echo “Shaken, “ . “not stirred”;   // Displays  “ Shaken, not stirred ”

                  You can also concatenate more than two strings at once. Furthermore, the values you concatenate don ’ t
                have to be strings; thanks to PHP ’ s automatic type conversion, non - string values, such as integers and
                floats, are converted to strings at the time they ’ re concatenated:

                    $tempF = 451;
                    // Displays  “ Books catch fire at 232.777777778 degrees C. ”
                    echo “Books catch fire at “ . ( (5/9) * ($tempF - 32) ) . “ degrees C.”;


              46





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