PHP3 Manual | ||
---|---|---|
Prev | Chapter 7. Expressions | Next |
Remember basic arithmetic from school? These work just like those.
Table 7-1. 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. |
The division operator ("/") returns an integer value (the result of an integer division) if the two operands are integers (or strings that get converted to integers). If either operand is a floating-point value, floating-point division is performed.
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 7-2. 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. |
Table 7-3. 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 7-4. 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. |
Prev | Home | Next |
Expressions | Up | Function Reference |