PHP3 Manual
PrevChapter 7. ExpressionsNext

Operators

Arithmetic Operators

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

Table 7-1. 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.

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.

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 7-2. 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.

Logical Operators

Table 7-3. 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 7-4. 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.

Precedence


PrevHomeNext
ExpressionsUpFunction Reference