Logical operators


If you haven’t used them before, logical operators may at first seem a little daunting.

Operator     Description         Example
&&           And                 $j == 3 && $k == 2
and          Low-precedence and  $j == 3 and $k == 2
||           Or                  $j < 5 || $j > 10
or           Low-precedence or   $j < 5 or $j > 10
!            Not !               ($j == $k)
xor          Exclusive or        $j xor $k

Note that && is usually interchangeable with and; the same is true for || and or. However, because and and or have a lower precedence you should avoid using them except when they are the only option, as in the following statement, which must use the or operator (|| cannot be used to force a second statement to execute if the first fails):

$html = file_get_contents($site) or die(“Cannot download from $site”);

The most unusual of these operators is xor, which stands for exclusive or and returns a TRUE value if either value is TRUE, but a FALSE value if both inputs are TRUE or both inputs are FALSE. To understand this, imagine that you want to concoct your own cleaner for household items. Ammonia makes a good cleaner, and so does bleach, so you want your cleaner to have one of these. But the cleaner must not have both,
because the combination is hazardous. In PHP, you could represent this as follows:

$ingredient = $ammonia xor $bleach;

In this example, if either $ammonia or $bleach is TRUE, $ingredient will also be set to TRUE. But if both are TRUE or both are FALSE, $ingredient will be set to FALSE.

Comparison operators

Comparison operators are generally used inside a construct such as an if statement in which you need to compare two items. For example, you may wish to know whether a variable you have been incrementing has reached a specific value, or whether another variable is less than a set value, and so on.

Operator   Description                 Example
==         Is equal to                 $j == 4
!=         Is not equal to             $j != 21
<          Is less than                $j < 100
>          Is greater than             $j > 3
>=         Is greater than or equal to $j >= 15
<=         Is less than or equal to    $j <= 8
<>         Is not equal to to          $j <> 23
===        Is identical to to          $j === "987"
!==        Is not identical to to      $j !== "1.2e3"

Note the difference between = and ==. The first is an assignment operator, and the second is a comparison operator. Even advanced programmers can sometimes mix up the two when coding hurriedly, so be careful. Remember, make just one bug per day 🙂

Operators

Operators let you specify mathematical operations to perform, such as addition, subtraction, multiplication, and division. But several other types of operators exist too, such as the string, comparison, and logical operators. Math in PHP looks a lot like plain arithmetic—for instance, the following statement outputs 8:

echo 6 + 2;

Before moving on to learn what PHP can do for you, take a moment to learn about the various operators it provides.
Arithmetic operators Arithmetic operators do what you would expect—they are used to perform mathematics.
You can use them for the main four operations (add, subtract, multiply, and divide) as well as to find a modulus (the remainder after a division) and to increment or decrement a value.

Operator Description    Example
+        Addition       $j + 1
-        Subtraction    $j - 6
*        Multiplication $j * 11
/        Division       $j / 4
%        Modulus        $j % 9
++       Increment      ++$j
--       Decrement      --$j
**       Exponentiation $j**2

Assignment operators
These operators assign values to variables. They start with the very simple = and move on to +=, -=, and so on.
The operator += adds the value on the right side to the variable on the left, instead of totally replacing the value on the left.
Thus, if $count starts with the value 5, the statement:
$count += 1;
sets $count to 6, just like the more familiar assignment statement:
$count = $count + 1;