Page 82 - Beginning PHP 5.3
P. 82

Part II: Learning the Language
                   Here ’ s a list of the comparison operators in PHP:

                      Operator                    Example         Result

                       ==  (equal)                 $x == $y        true  if  $x  equals  $y ;  false  otherwise
                       !=  or   < >   (not equal)         $x != $y        true  if  $x  does not equal  $y ;  false

                                                               otherwise
                       ===  (identical)           $x === $y        true  if  $x  equals  $y  and they are of the
                                                               same type;   false  otherwise

                       !==  (not identical)        $x !== $y        true  if  $x  does not equal  $y  or they are not
                                                               of the same type;   false  otherwise
                         <   (less than)          $x  <  $y         true  if  $x  is less than  $y ;  false  otherwise

                         >   (greater than)       $x  >  $y         true  if  $x  is greater than  $y ;  false
                                                               otherwise

                        < =  (less than or equal to)         $x  < = $y        true  if  $x  is less than or equal to  $y ;  false
                                                               otherwise
                         > =  (greater than or equal to)         $x  > = $y        true  if  $x  is greater than or equal to  $y ;
                                                                 false  otherwise


                  The following examples show comparison operators in action:
                    $x = 23;

                    echo ( $x  <  24 ) . “ < br / > ”;     // Displays 1 (true)
                    echo ( $x  <  “24 ”  ) . “ < br / > ”;   // Displays 1 (true) because
                                                     // PHP converts the string to an integer
                    echo ( $x == 23 ) . “ < br / > ”;    // Displays 1 (true)
                    echo ( $x === 23 ) . “ < br / > ”;   // Displays 1 (true)
                    echo ( $x === “23 ”  ) . “ < br / > ”; // Displays “” (false) because
                                                     // $x and “23” are not the same data type
                    echo ( $x  > = 23 ) . “ < br / > ”;    // Displays 1 (true)

                    echo ( $x  > = 24 ) . “ < br / > ”;    // Displays “” (false)
                   As you can see, comparison operators are commonly used to compare two numbers (or strings
                 converted to numbers). The = =  operator is also frequently used to check that two strings are the same.

                  Incrementing /Decrementing Operators
                  Oftentimes it ’ s useful to add or subtract the value 1 (one) over and over. This situation occurs so
                 frequently  —  for example, when creating loops  —  that special operators are used to perform this task:
                 the  increment  and  decrement  operators. They are written as two plus signs or two minus signs,

                 respectively, preceding or following a variable name, like so:
                    ++$x; // Adds one to $x and then returns the result
                    $x++; // Returns $x and then adds one to it
                      – - $x; // Subtracts one from $x and then returns the result



                    $x – - ; // Returns $x and then subtracts one from it
              44



                                                                                                      9/21/09   8:51:25 AM
          c03.indd   44
          c03.indd   44                                                                               9/21/09   8:51:25 AM
   77   78   79   80   81   82   83   84   85   86   87