## Operators

Now that we have data values to work with, we need some operators to use, too. We have already used + to add variables together, but many others in PHP handle arithmetic, comparison, assignment, and other operators. Operator is just a fancy word for something that performs an operation, such as addition or subtraction. However, operand might be new to you. Consider this operation:

In this operation, = and + are operators, and \$a, \$b, and \$c are operands. Along with +, you will also already know (subtract), * (multiply), and / (divide), but here are some more.

Operator What It Does

= Assigns the right operand to the left operand.

== Returns TRue if the left operand is equal to the right operand.

!= Returns true if the left operand is not equal to the right operand.

=== Returns TRue if the left operand is identical to the right operand. This is not the same as

!== Returns true if the left operand is not identical to the right operand. This is not the same as !=.

< Returns true if the left operand is smaller than the right operand.

> Returns TRue if the left operand is greater than the right operand.

<= Returns true if the left operand is equal to or smaller than the right operand.

&& Returns true if both the left operand and the right operand are true.

II Returns TRue if either the left operand or the right operand is true.

++ Increments the operand by one.

-- Decrements the operand by one.

+= Increments the left operand by the right operand.

-= Decrements the left operand by the right operand.

. Concatenates the left operand and the right operand (joins them together).

% Divides the left operand by the right operand and returns the remainder.

I Performs a bitwise or operation. It returns a number with bits that are set in either the left operand or the right operand.

& Performs a bitwise and operation. It returns a number with bits that are set both in the left operand and the right operand.

At least 10 other operators are not listed; to be fair, however, you're unlikely to use them. Even some of the ones in this list are used infrequentlybitwise and, for example. Having said that, the bitwise or operator is used regularly because it allows you to combine values.

Here is a code example demonstrating some of the operators:

\$j = \$i; // both \$j and \$i are 55 \$i = \$j % 11; // \$i is 0

The last line uses modulus, which takes some people a little bit of effort to understand. The result of \$i % 11 is 0 because \$i is set to 55 and modulus works by dividing the left operand (55) by the right operand (11) and returning the remainder. 55 divides by 11 exactly 5 times, and so has the remainder 0.

The concatenation operator, a period, sounds scarier than it is: It just joins strings together. For example:

<?php echo "Hello, " . "world!"; echo "Hello, world!" . "\n";

There are two "special" operators in PHP that are not covered here and yet are used frequently. Before we look at them, though, it's important that you see how the comparison operators (such as <, <=, and !=) are used inside conditional statements.

0 0