back || home || next

Comparison Operators

Comparison operators perform tests on their operands. They return the Boolean value true if the test is successful, or false otherwise. This type of expression is useful in control structures, such as if and while statements.

 

To test whether the value contained in $v is smaller than 2, for example, you would use the less than operator:

$v < 2

If $v contained 1, this expression would be equivalent to the value true. If $v contained 7, the expression would resolve to false.

 

Operator

Name

Returns true if

Example

==

Equivalence

Left is equivalence to right

$v == 5

!=

Non-equivalence

Left is not equivalence to right

$v != 5

===

Identical

Left is equivalence to right and are in same type

$v ===5

> 

Greater than

Left is greater than right

$v > 5

>=

Greater than or equal to

Left is greater than right or equal to right

$v >= 5

< 

Less than

Left is less than right

$v < 5

<=

Less than or equal to

Left is less than or equal to right

$v <= 5

 

 

Creating More Complex Test Expressions with the Logical Operators

The logical operators test combinations of booleans. The or operator, for example returns true if either the left or the right operand is true.

 

true || false

 

would return true. The and operator only returns true if both the left and right operands are true.

 

true && false

 

would return false. It's unlikely that you would use a logical operator to test Boolean constants, however. It would make more sense to test two or more expressions that resolve to a boolean. For example,

 

( $x > 2 ) && ( $x < 15 )

 

would return true if $x contained a value that is greater than 2 and smaller than 15. We include the parentheses to make the code easier to read.

Operator

Name

True if

Example

||

Or

Left or right is true

true || false

or

Or

Left or right is true

true || false

xor

Xor

Left or right is true bur not both

true || true

&&

And

Left and right are true

true && false

!

Not

The single operand is not true

!true

 

 

Automatically Incrementing and Decrementing an Integer Variable

When coding in PHP, you will often find it necessary to increment or decrement an integer variable. You will usually need to do this when you are counting the iterations of a loop. You have already learned two ways of doing this. I could increment the integer contained by $x with the addition operator

 

$x = $x + 1; // $x is incremented

 

or with a combined assignment operator

 

$x += 1; // $x is incremented

 

In both cases, the resultant integer is assigned to $x. Because expressions of this kind are so common, PHP provides some special operators that allow you to add or subtract the integer constant 1 from an integer variable, assigning the result to the variable itself. These are known as the post-increment and post-decrement operators. The post-increment operator consists of two plus symbols appended to a

variable name.

 

$x++; // $x is incremented

 

increments the variable $x by one. Using two minus symbols in the same way decrements the variable:

 

$x− − ; // $x is decremented

 

If you use the post-increment or post-decrement operators in conjunction with a conditional operator, the operand will only be modified after the test has been completed:

 

$x = 3;

$x++ < 4; // true

 

In the previous example, $x contains 3 when it is tested against 4 with the less than operator, so the test expression returns true. After this test is complete, $x is incremented.

In some circumstances, you might want to increment or decrement a variable in a test expression before the test is carried out. PHP provides the pre -increment and pre-decrement operators for this purpose. On their own, these operators behave in exactly the same way as the post-increment and post-decrement operators. They are written with the plus or minus symbols preceding the variable:

 

++$x; // $x is incremented

− − $x; // $x is decremented

 

If these operators are used as part of a test expression, the incrementation occurs before the test is carried out.

 

$x = 3;

++$x < 4; // false

 

In the previous fragment, $x is incremented before it is tested against 4. The test expression returns false because 4 is not smaller than 4.

 

Operator Precedence

When you use an operator, the interpreter usually reads your expression from left to right. For complex expressions that use more than one operator, though, the waters can become a little murky. First, consider a simple case:

 

4 + 5

 

There's no room for confusion, here. PHP simply adds 4 to 5. What about the next fragment?

 

4 + 5 * 2

 

This presents a problem. Does it mean the sum of 4 and 5, which should then be multiplied by 2, giving the result 18? Does it mean 4 plus the result of 5 multiplied by 2, resolving to 14? If you were to read simply from left to right, the former would be true. In fact, PHP attaches different precedence to operators. Because the multiplication operator has higher precedence than the addition operator does, the second solution to the problem is the correct one.

You can force PHP to execute the addition expression before the multiplication expression with parentheses:

 

( 4 + 5 ) * 2

 

Whatever the precedence of the operators in a complex expression, it is a good idea to use parentheses to make your code clearer and to save you from obscure bugs. As you can see, or has a lower precedence than || and and has a lower precedence than &&, so you could use the lower-precedence logical operators to change the way a complex test expression is read. This is not necessarily a good idea. The following two expressions are equivalent, but the second is much easier to read:

 

$x and $y || $z

bac