News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

PHP Tutorial – 04 – Operators

Operators are used to operate on values. They can be grouped into five types: arithmetic, assignment, comparison, logical and bitwise operators.

Arithmetic operators

The arithmetic operators include the four basic arithmetic operations, as well as the modulus operator (%) which is used to obtain the division remainder.

$x = 4 + 2; // 6 // addition
$x = 4 - 2; // 2 // subtraction
$x = 4 * 2; // 8 // multiplication
$x = 4 / 2; // 2 // division
$x = 4 % 2; // 0 // modulus (division remainder)

Assignment operators

The second group is the assignment operators. Most importantly, the assignment operator (=) itself, which assigns a value to a variable.

Combined assignment operators

A common use of the assignment and arithmetic operators is to operate on a variable and then to save the result back into that same variable. These operations can be shortened with the combined assignment operators.

$x = 0;
$x += 5; // $x = $x+5;
$x -= 5; // $x = $x-5;
$x *= 5; // $x = $x*5; 
$x /= 5; // $x = $x/5;
$x %= 5; // $x = $x%5;

Increment and decrement operators

Another common operation is to increment or decrement a variable by one. This can be simplified with the increment (++) and decrement (--) operators.

$x++; // $x += 1;
$x--; // $x -= 1;

Both of these operators can be used either before or after a variable.

$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement

The result on the variable is the same whichever is used. The difference is that the post-operator returns the original value before it changes the variable, while the pre-operator changes the variable first and then returns the value.

$x = 5; $y = $x++; // $x=6, $y=5
$x = 5; $y = ++$x; // $x=6, $y=6

Comparison operators

The comparison operators compare two values and return either true or false. They are mainly used to specify conditions, which are expressions that evaluate to either true or false.

$x = (2 == 3);  // false // equal to
$x = (2 != 3);  // true  // not equal to
$x = (2 <> 3);  // true  // not equal to (alternative)
$x = (2 === 3); // false // identical
$x = (2 !== 3); // true  // not identical
$x = (2 > 3);   // false // greater than
$x = (2 < 3);   // true  // less than
$x = (2 >= 3);  // false // greater than or equal to
$x = (2 <= 3);  // true  // less than or equal to

The identical operator (===) is used for comparing both the value and data type of the operands. It returns true if both operands have the same value and are of the same type. Likewise, the not identical operator (!==) returns true if the operands do not have the same value or are not of the same type. Put another way, the equality operators will perform type conversions, whereas the identical operators will not.

$x = (1 ==  "1"); // true  (same value)
$x = (1 === "1"); // false (different types)

Logical operators

The logical operators are often used together with the comparison operators. Logical and (&&) evaluates to true if both the left and right side are true, and logical or (||) evaluates to true if either the left or right side is true. The logical not (!) operator is used for inverting a Boolean result. Note that for both “logical and” and “logical or” the right side of the operator will not be evaluated if the result is already determined by the left side.

$x = (true && false); // false // logical and
$x = (true || false); // true  // logical or
$x = !(true);         // false // logical not

Bitwise operators

The bitwise operators can manipulate binary digits of numbers. For example, the xor operator (^) turn on the bits that are set on one side of the operator, but not on both sides.

$x = 5 & 4;  // 101 & 100 = 100 (4) // and
$x = 5 | 4;  // 101 | 100 = 101 (5) // or
$x = 5 ^ 4;  // 101 ^ 100 = 001 (1) // xor (exclusive or)
$x = 4 << 1; // 100 << 1  =1000 (8) // left shift
$x = 4 >> 1; // 100 >> 1  =  10 (2) // right shift
$x = ~4;     // ~00000100 = 11111011 (-5) // invert

These bitwise operators have shorthand assignment operators, just like the arithmetic operators.

$x=5; $x &= 4;  // 101 & 100 = 100 (4) // and
$x=5; $x |= 4;  // 101 | 100 = 101 (5) // or
$x=5; $x ^= 4;  // 101 ^ 100 = 001 (1) // xor
$x=5; $x <<= 1; // 101 << 1  =1010 (10)// left shift
$x=5; $x >>= 1; // 101 >> 1  =  10 (2) // right shift

Operator precedents

In PHP, expressions are normally evaluated from left to right. However, when an expression contains multiple operators, the precedence of those operators decides the order in which they are evaluated.

PreOperatorPreOperator
1++ —10^
2~ – (unary)11|
3!12&&
4* / %13||
5+ – (binary)14= op=
6<< >>15and
7< <= > >= <>16xor
8== != === !==17or
9&  

For example, logical and (&&) binds weaker than relational operators, which in turn bind weaker than arithmetic operators.

$x = 2+3 > 1*4 && 5/5 == 1; // true

To make things clearer, parentheses can be used to specify which part of the expression will be evaluated first. Parentheses have the highest precedence of all operators.

$x = ((2+3) > (1*4)) && ((5/5) == 1); // true

Additional logical operators

In the precedence table make special note of the last three operators: and, or and xor. The and and or operators work in the same way as the logical && and || operators. The only difference is their lower level of precedence.

// Same as: $a = (true && false);
$a = true && false; // $a is false
 
// Same as: ($a = true) and false;
$a = true and false; // $a is true

The xor operator is a Boolean version of the bitwise ^ operator. It evaluates to true if only one of the operands are true.

$a = (true xor true); // false
Recommended additional reading:
Sams - Teach Yourself PHP in 24 Hours