Operators

Arithmetic Operators

Remember basic arithmetic from school? These work just like those.

Table 5-2. Arithmetic Operators

examplenameresult
$a + $bAdditionSum of $a and $b.
$a - $bSubtractionRemainder of $b subtracted from $a.
$a * $bMultiplicationProduct of $a and $b.
$a / $bDivisionDividend of $a and $b.
$a % $bModulusRemainder of $a divided by $b.

String Operators

There is only really one string operator -- the concatenation operator (".").

$a = "Hello ";
$b = $a . "World!"; // now $b = "Hello World!"
     

Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the the left operand gets set to the value of the expression on the rights (that is, "gets set to").

The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:

$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.

In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example:

$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

Bitwise Operators

Bitwise operators allow you to turn specific bits within an integer on or off.

Table 5-3. Bitwise Operators

examplenameresult
$a & $bAndBits that are set in both $a and $b are set.
$a | $bOrBits that are set in either $a or $b are set.
~ $aNotBits that are set in $a are not set, and vice versa.
$a << $bShift leftShift the bits of $a $b steps to the left (each step means "multiply by two")
$a >> $bShift rightShift the bits of $a $b steps to the right (each step means "divide by two")

Logical Operators

Table 5-4. Logical Operators

examplenameresult
$a and $bAndTrue of both $a and $b are true.
$a or $bOrTrue if either $a or $b is true.
$a xor $bOrTrue if either $a or $b is true, but not both.
! $aNotTrue if $a is not true.
$a && $bAndTrue of both $a and $b are true.
$a || $bOrTrue if either $a or $b is true.

The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See below.)

Comparison Operators

Comparison operators, as their name imply, allow you to compare two values.

Table 5-5. Comparson Operators

examplenameresult
$a == $bEqualTrue if $a is equal to $b.
$a != $bNot equalTrue if $a is not equal to $b.
$a < $bLess thanTrue if $a is strictly less than $b.
$a > $bGreater thanTrue if $a is strictly greater than $b.
$a <= $bLess than or equal to True if $a is less than or equal to $b.
$a >= $bGreater than or equal to True if $a is greater than or equal to $b.

Another conditional operator is the "?:" (or trinary) operator, which operates as in C and many other languages.

(expr1) ? (expr2) : (expr3);

This expression returns to expr2 if expr1 evalutes to true, and expr3 if expr1 evaluates to false.

Operator Precedence

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator.

The following table lists the precedence of operators with the lowest-precedence operators listed first.

Table 5-6. Operator Precedence

AssociativityOperators
left,
leftor
leftxor
leftand
rightprint
left= += -= *= /= .= %= &= != ~= <<= >>=
left? :
left||
left&&
left|
left^
left&
non-associative== !=
non-associative< <= > >=
left<< >>
left+ - .
left* / %
right! ~ ++ -- (int) (double) (string) (array) (object) @
right[
non-associativenew