PHP offers a lot of powerful operators, ranging from arithmetic, string, and logical operators to operators for assignment, comparison, and more (see Table 41).
Table 41. PHP operator types
Operator  Used for  Example 

Arithmetic  Basic mathematics 

Array  Array union 

Assignment  Assigning values 

Bitwise  Manipulating bits within bytes 

Comparison  Comparing two values 

Execution  Executing contents of backticks 

Increment/Decrement  Adding or subtracting 1 

Logical  Boolean comparisons 

String  Concatenation 

Different types of operators take a different number of operands:
Unary operators, such as incrementing
($a++
) or negation ($a
), take a single operand.
Binary operators, which represent the bulk of PHP operators (including addition, subtraction, multiplication, and division), take two operands.
There is one ternary operator, which takes
the form x ? y : z
. It’s a terse,
singleline if
statement that
chooses between two expressions, depending on the result of a third
one. This conditional operator takes three operands.
If all operators had the same precedence, they would be processed in the order in which they are encountered. In fact, many operators do have the same precedence—Example 45 illustrates one such case.
Here you will see that although the numbers (and their preceding
operators) have been moved around, the result of each expression is the
value 7
, because the plus and minus
operators have the same precedence. We can try the same thing with
multiplication and division (see Example 46).
Example 46. Three expressions that are also equivalent
1 * 2 * 3 / 4 * 5 2 / 4 * 5 * 3 * 1 5 * 2 / 4 * 1 * 3
Here the resulting value is always 7.5
. But things change when we mix operators
with different precedences in an expression, as in
Example 47.
Example 47. Three expressions using operators of mixed precedence
1 + 2 * 3  4 * 5 2  4 * 5 * 3 + 1 5 + 2  4 + 1 * 3
If there were no operator precedence, these three expressions
would evaluate to 25
, −29
, and 12
, respectively. But because multiplication
and division take precedence over addition and subtraction, there are
implied parentheses around these parts of the expressions, which would
look like Example 48 if
they were visible.
Example 48. Three expressions showing implied parentheses
1 + (2 * 3)  (4 * 5) 2  (4 * 5 * 3) + 1 5 + 2  4 + (1 * 3)
Clearly, PHP must evaluate the subexpressions within parentheses first to derive the semicompleted expressions in Example 49.
Example 49. After evaluating the subexpressions in parentheses
1 + (6)  (20) 2  (60) + 1 5 + 2  4 + (3)
The final results of these expressions are −13
, −57
,
and 6
, respectively (quite different
from the results of 25
, −29
, and 12
that we would have seen had there been no operator precedence).
Of course, you can override the default operator precedence by inserting your own parentheses and force the original results that we would have seen, had there been no operator precedence (see Example 410).
Example 410. Forcing lefttoright evaluation
((1 + 2) * 3  4) * 5 (2  4) * 5 * 3 + 1 (5 + 2  4 + 1) * 3
With parentheses correctly inserted, we now see the values
25
, −29
, and 12
, respectively.
Table 42 lists PHP’s operators in order of precedence from high to low.
Table 42. The precedence of PHP operators (high to low)
Operator(s)  Type 

 Parentheses 
 Increment/Decrement 
 Logical 
 Arithmetic 
 Arithmetic and string 
 Bitwise 
 Comparison 
 Comparison 
 Bitwise (and references) 
 Bitwise 
 Bitwise 
 Logical 
 Logical 
 Ternary 
 Assignment 
 Logical 
 Logical 
 Logical 
We’ve been looking at processing expressions from left to right, except where operator precedence is in effect. But some operators can also require processing from right to left. The direction of processing is called the operator’s associativity.
This associativity becomes important in cases in which you do not explicitly force precedence. Table 43 lists all the operators that have righttoleft associativity.
Table 43. Operators with righttoleft associativity
Operator  Description 

 Create a new object 
 Logical NOT 
 Bitwise NOT 
 Increment and decrement 
 Unary plus and negation 
 Cast to an integer 
 Cast to a float 
 Cast to a string 
 Cast to an array 
 Cast to an object 
 Inhibit error reporting 
 Assignment 
For example, let’s take a look at the assignment operator in Example 411, where three variables are
all set to the value 0
.
This multiple assignment is possible only if the rightmost part of the expression is evaluated first and then processing continues in a righttoleft direction.
As a PHP beginner, you should learn to avoid the potential pitfalls of operator associativity by always nesting your subexpressions within parentheses to force the order of evaluation. This will also help other programmers who may have to maintain your code to understand what is happening.
Relational operators test two operands and
return a Boolean result of either TRUE
or FALSE
. There are three types of relational
operators: equality,
comparison, and logical
operators.
The equality operator, which we’ve already encountered a few
times in this chapter, is ==
(two
equals signs). It is important not to confuse it with the =
(single equals sign) assignment operator.
In Example 412, the
first statement assigns a value and the second tests it for
equality.
Example 412. Assigning a value and testing for equality
<?php $month = "March"; if ($month == "March") echo "It's springtime"; ?>
As you see, returning either TRUE
or FALSE
, the equality operator enables you to
test for conditions using, for example, an if
statement. But that’s not the whole
story, because PHP is a loosely typed language. If the two operands of
an equality expression are of different types, PHP will convert them
to whatever type makes best sense to it.
For example, any strings composed entirely of numbers will be
converted to numbers whenever compared with a number. In Example 413, $a
and $b
are two different strings and we would therefore expect neither of the
if
statements to output a
result.
Example 413. The equality and identity operators
<?php $a = "1000"; $b = "+1000"; if ($a == $b) echo "1"; if ($a === $b) echo "2"; ?>
However, if you run the example, you will see that it outputs
the number 1, which means that the first if
statement evaluated to TRUE
. This is because both strings were
first converted to numbers, and 1000 is the same numerical value as
+1000.
In contrast, the second if
statement uses the identity operator—three equals
signs in a row—which prevents PHP from automatically converting types.
$a
and $b
are therefore compared as strings and are
now found to be different, so nothing is output.
As with forcing operator precedence, whenever you feel there may be doubt about how PHP will convert operand types, you can use the identity operator to turn off this behavior.
In the same way that you can use the equality operator to test
for operands being equal, you can test for them
not being equal using !=
, the inequality
operator. Take a look at Example 414, which is a
rewrite of Example 413 in
which the equality and identity operators have been replaced with
their inverses.
Example 414. The inequality and not identical operators
<?php $a = "1000"; $b = "+1000"; if ($a != $b) echo "1"; if ($a !== $b) echo "2"; ?>
As you might expect, the first if
statement does not output the number 1,
because the code is asking whether $a
and $b
are not equal to each other numerically.
Instead, it outputs the number 2, because the second if
statement is asking whether $a
and $b
are not identical to each other in their present
operand types, and the answer is TRUE
; they are not the same.
Using comparison operators, you can test for more than just
equality and inequality. PHP also gives you >
(is greater than), <
(is less than), >=
(is greater than or equal to), and
<=
(is less than or equal to) to
play with. Example 415 shows
these operators in use.
Example 415. The four comparison operators
<?php $a = 2; $b = 3; if ($a > $b) echo "$a is greater than $b<br />"; if ($a < $b) echo "$a is less than $b<br />"; if ($a >= $b) echo "$a is greater than or equal to $b<br />"; if ($a <= $b) echo "$a is less than or equal to $b<br />"; ?>
In this example, where $a
is
2
and $b
is 3
,
the following is output:
2 is less than 3 2 is less than or equal to 3
Try this example yourself, altering the values of $a
and $b
, to see the results. Try setting them to
the same value and see what happens.
Logical operators produce trueorfalse results, and therefore are also known as Boolean operators. There are four of them (see Table 44).
Table 44. The logical operators
Logical operator  Description 









You can see these operators used in Example 416. Note that the !
symbol is required by PHP in place of the
word NOT
. Furthermore, the
operators can be lower or uppercase.
Example 416. The logical operators in use
<?php $a = 1; $b = 0; echo ($a AND $b) . "<br />"; echo ($a or $b) . "<br />"; echo ($a XOR $b) . "<br />"; echo !$a . "<br />"; ?>
This example outputs NULL
,
1
, 1
, NULL
,
meaning that only the second and third echo
statements evaluate as TRUE
. (Remember that NULL
—or nothing—represents a value of
FALSE
.) This is because the
AND
statement requires both
operands to be TRUE
if it is going
to return a value of TRUE
, while
the fourth statement performs a NOT
on the value of $a
, turning it from
TRUE
(a value of 1
) to FALSE
. If you wish to experiment with this,
try out the code, giving $a
and
$b
varying values of 1
and 0
.
When coding, remember to bear in mind that AND
and OR
have lower precedence than the other
versions of the operators, &&
and 
. In complex expressions, it may be
safer to use &&
and

for this reason.
The OR
operator can cause
unintentional problems in if
statements, because the second operand will not be evaluated if the
first is evaluated as TRUE
. In
Example 417, the function
getnext
will never be called if
$finished
has a value of 1
.
If you need getnext
to be
called at each if
statement, you
could rewrite the code as has been done in Example 418.
Example 418. The if…OR statement modified to ensure calling of getnext
<?php $gn = getnext(); if ($finished == 1 OR $gn == 1) exit; ?>
In this case, the code in function getnext
will be executed and the value
returned stored in $gn
before the
if
statement.
Another solution is to simply switch the two clauses to make
sure that getnext
is executed, as
it will then appear first in the expression.
Table 45 shows
all the possible variations of using the logical operators. You should
also note that !TRUE
equals
FALSE
and !FALSE
equals TRUE
.