Remember basic arithmetic from school? These work just like those.
Table 5-2. Arithmetic Operators
example | name | result |
---|---|---|
$a + $b | Addition | Sum of $a and $b. |
$a - $b | Subtraction | Remainder of $b subtracted from $a. |
$a * $b | Multiplication | Product of $a and $b. |
$a / $b | Division | Dividend of $a and $b. |
$a % $b | Modulus | Remainder of $a divided by $b. |
There is only really one string operator -- the concatenation operator (".").
$a = "Hello "; $b = $a . "World!"; // now $b = "Hello World!"
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 allow you to turn specific bits within an integer on or off.
Table 5-3. Bitwise Operators
example | name | result |
---|---|---|
$a & $b | And | Bits that are set in both $a and $b are set. |
$a | $b | Or | Bits that are set in either $a or $b are set. |
~ $a | Not | Bits that are set in $a are not set, and vice versa. |
$a << $b | Shift left | Shift the bits of $a $b steps to the left (each step means "multiply by two") |
$a >> $b | Shift right | Shift the bits of $a $b steps to the right (each step means "divide by two") |
Table 5-4. Logical Operators
example | name | result |
---|---|---|
$a and $b | And | True of both $a and $b are true. |
$a or $b | Or | True if either $a or $b is true. |
$a xor $b | Or | True if either $a or $b is true, but not both. |
! $a | Not | True if $a is not true. |
$a && $b | And | True of both $a and $b are true. |
$a || $b | Or | True 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, as their name imply, allow you to compare two values.
Table 5-5. Comparson Operators
example | name | result |
---|---|---|
$a == $b | Equal | True if $a is equal to $b. |
$a != $b | Not equal | True if $a is not equal to $b. |
$a < $b | Less than | True if $a is strictly less than $b. |
$a > $b | Greater than | True if $a is strictly greater than $b. |
$a <= $b | Less than or equal to | True if $a is less than or equal to $b. |
$a >= $b | Greater 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);
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
Associativity | Operators |
---|---|
left | , |
left | or |
left | xor |
left | and |
right | |
left | = += -= *= /= .= %= &= != ~= <<= >>= |
left | ? : |
left | || |
left | && |
left | | |
left | ^ |
left | & |
non-associative | == != |
non-associative | < <= > >= |
left | << >> |
left | + - . |
left | * / % |
right | ! ~ ++ -- (int) (double) (string) (array) (object) @ |
right | [ |
non-associative | new |