April 5, 2010

Types Of Operators in PHP


There are different types of operators in PHP. Variables, constants, and data types will not be very beneficial to you until you can’t merge and manipulate them in a numerous ways. In PHP, it can be done easily by using operators. PHP recognizes several classes of operators, depending on what purpose they are used for. We can list the operators as follows:

The Assignment Operator
The assignment operator '=' is used to assign a value to a variable:
$x = 65;
$y = “Joseph is a good boy”;
It’s vital to understand that, by default, variables are assigned by value. This means that by doing following:
$x = $y
It will assigns the value of $y to $x. But, if you change $y after assigning, $x will remain the same. Sometimes you don't want to do this and want that when you change the value of $y, it is also been reflected in $x. You can do it easily by assigning to $x a reference to $y:
$x = &$y
Any change to $y will now also be reflected in $x.


Arithmetic Operators
The next operator in this category is Arithmetic operators. These operators are used to do arithmetic calculations like addition, subtraction etc. Arithmetic operators are always been used in binary operations (means that they always include two operators):
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
In PHP modulus operation works a bit different from the typical mathematical operation as it returns a signed value rather than an absolute one. PHP also inherits 4 special incrementing/decrementing operators from the C language.

The prefix incrementing operator ++ increases the value of the variable that succeeds it, and then returns its new value. For example, ++$x will firstly increase the value of $x by 1 and then return it.

The postfix incrementing operator ++ returns the value of the variable that pre-cedes it, and then increments its value. For example, $x++ will instantly returns the value of $x and after that it increases the value of $x by 1 for the next operation.

The prefix decrementing operator --decrements the value of the variable that succeeds it, and then returns its new value. For example, --$x will first decreases the value of $x and then returns it.

The postfix decrementing operator -- returns the value of the variable that precedes it, and then decrements its value. For example, $x-- will instantly returns the value of $x and after that it decreases the value of $x by 1 for the next operation.

There is always a great confusion in between these operators, but if you follow a simple rule then it wil be very easy for you. The rule is that if  the variable is placed after the operator (Prefix version) then you must increase or decrease the value first.
But if the variable is placed before the operator (Postfix version) then you must returns the value first and after that increase or decrease the value of the variable.


Bitwise Operators
The next operator in this category is Bitwise operator. It manipulates the value of variables at the bit level:

The bitwise AND (&) operation causes the value of a bit to be set if it is set in both the left and right operands.
For example, 1 & 1 = 1, whereas 1 & 2 = 0

The bitwise OR (|) operation causes the value of a bit to be set if it is set in either the left or right operands.
For example, 1 | 1 = 1 and 1 | 2 = 3

The bitwise XOR (^) operation causes the value of a bit to be set if it is set in either the left or right operands, but not in both.
For example, 1 ^ 1 = 0, 1 ^ 0 = 1

The bitwise NOT (~) operation causes the bits in its operand to be reversed—that is, set if they are not and unset otherwise. The bitwise left-shift (<<) and right-shift (>>) operators actually shift the bits of the left operands left or right by the number of positions specified by the right operand.


Comparison Operators
The next operator in this category is Comparison operator. Comparison operators are used to establish the relationship between two operands. The result of a comparison is always a Boolean value:

The = = operator determines if two values are equal. For example:
$x = = $y where both $x and $y are equal to 10

The != operator determines if two values are different. For example:
$x != $y where $x = 15 and $y=18

The < operator determines whether the left operand’s value is less than the right operand’s. For example:
$x < $y where $x = 25 and $y=67

The > operator determines whether the left operand’s value is greater than the right operand’s. For example:
$x > $y where $x = 85 and $y=62

The <= operator determines whether the left operand’s value is less than or equal to the right operand’s. For example:
$x <= $y where $y = 85 and $x=85 or less than 85

The >= operator determines whether the left operand’s value is greater than the right operand’s. For example:
$x >= $y where $x = 28 and $y=28 or less than 28


Logical Operators
The next operator in this category is Logical operator. Logical operators are generally used in conjunction with comparison operators to create complex decision process. They also return a Boolean result:

The AND operator (indicated by the keyword and or by &&) returns True if both the left and right operands cannot be evaluated to False. For example:
($x < $y) && ($x > $z )
returns true if  $x = 25 , $y=67 and $z=10

The OR operator  (indicated by the keyword or or by ||) returns True if either the left or right operand cannot be evaluated as False. For example:
($x < $y) || ($x > $z )
returns true if  $x = 25 , $y=67 and $z=90

The XOR operator (indicated by the keyword xor) returns True if either the left or right operand can be evaluated as True, but not both. For example:
($x < $y) || ($x > $z )
returns False if  $x = 25 , $y=67 and $z=15

The unary NOT operator (indicated by !) returns False if the operand can be evaluated as True, and True otherwise.


Precedence of PHP Operators

Operator precedence determines the grouping of terms in an expression. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example x = 8 + 6 * 9; Here x is assigned 62, not 126 because operator * has higher precedence than + so it first get multiplied with 6*9 and then adds into 8.

You can easily understand the precedence of operators with the following table:

Category

Operator

Associativity

Unary

     ! ++ --

Right to left

Multiplicative

     * / %

Left to right

Additive

       + -

Left to right

Relational

  < <= > >=

Left to right

Equality

      = = !=

Left to right

Logical AND
&&
Left to right

Logical OR

        ||

Left to right

Conditional

        ?:

Right to left

Assignment

= += -= *= /= %=

Right to left


Share/Bookmark