News & UpdatesProgrammingWeb programmingPartnersStore
Book Cover
Buy Now
Projects
Links

PHP Tutorial – 04 – Operators

Operators are used to operate on values. There are basically five types: arithmetic, assignment, comparison, logical, and bitwise operators.

Arithmetic operators

There are the four basic arithmetic operations, as well as modulus to get the division remainder. The equals sign itself is an assignment operator which assigns a value to a variable.

$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)

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 += 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;

They can be used either before or after a variable.

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

The effect 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;
echo $a++; // 5
echo $a;   // 6  
 
$x = 5;
echo ++$a; // 6
echo $a;   // 6

Comparison operators

The comparison operators compare two values and return 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 the same type. Likewise, the not identical operator (!==) returns true if the operands don’t have the same value or are not of the same type.

Logical operators

The logical operators are often used together with the comparison operators. Logical AND (&&) and logical OR (||) can combine several comparisons and the NOT operator (!) inverts the result.

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

Bitwise operators

The bitwise operators work on the binary representation of integers. For example, the xor operator (^) turn on the bits that are set on one side of the operator, but not on both sides.

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

Operator precedence

Expressions are normally evaluated from left to right. However, as can be seen in the table below, different operators also have different precedents.

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

For example, the multiplication sign binds harder than addition and is therefore evaluated first.

$x = 2 + 1 * 2; // 4

To avoid having to learn the precedents of all operators you can instead use parenthesis to decide what part of the expression will be evaluated first.

$x = (2 + 1) * 2; // 6

More logical operators

In the precedence table make special note of the last three operators. The “and” and “or” operators work the same way as the logical operators && and ||. 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 (exclusive or) operator is a boolean version of the ^ operator. It evaluates to true if only one of the operands are true.

true xor true; // false

If you like this tutorial please +1 it:


Leave a Reply