Search This Blog

Wednesday, 13 February 2013

Learning More on Operators in PHP

0 comments

More Operators in PHP

Auto-increment and Auto-decrement Operators
Auto-increment (++) and Auto-decrement (--) operators are unary operators, which increments and decrements the value of a variable by one. These operators are unique in that they work only on variables; the operators change their operands’ values and return the new value.
These operators can be used in two ways: prefix notation and postfix notation. If we put the operator in front of the operand, it returns the new value of the operand (incremented or decremented). If we put the operator after the operand, it returns the original value of the operand (before the increment or decrement occurs). For e.g.:
$count = 10;
echo $count++;
echo ++count;
The above set of statements will display the values 10 and 12 during execution. Because the first echo statement will return the value of variable $count before increment, whereas the second echo will display the value of $count after increment.
Auto-increment and Auto-decrement operators can be applied to strings as well as numbers. Incrementing an alphabetic character turns it into the next letter in the alphabet. The following table gives some examples for auto-incrementing with letters:
Incrementing this Gives this
“a” “b”
“z” “aa”
“spaz” “spba”
“K9” “L0”
“42” “43”
Comparison Operators:
Comparison operators are binary operators, which compare two operands and result in either true, if the comparison is truthful or false otherwise. Comparison operators can be applied on two numbers, two strings or one number and one string.
The comparison of operands is done either using numeric order or lexicographic (textual) order. This depends on the types and values of operands used in the comparison. The following table summarizes the type of comparison performed by the comparison operators:
First Operand
Second Operand
Type of Comparison
Number
Number
Numeric
String that is entirely numeric (Numeric String)
String that is entirely numeric (Numeric String)
Numeric
String that is entirely numeric (Numeric String)
Number
Numeric
String that is not entirely numeric (Non-numeric String)
Number
Lexicographic
String that is entirely numeric (Numeric String)
String that is not entirely numeric (Non-numeric String)
Lexicographic
String that is not entirely numeric (Non-numeric String)
String that is not entirely numeric (Non-numeric String)
Lexicographic
Table : Type of comparison performed by Comparison Operators
The following are the comparison operators in PHP:
equality (==, :==) Returns true if both operands are equal
identity (===, :===) Returns true if both operands are equal and are of the same data type. These operators do not do implicit type casting.

inequality (!=, <>) Returns true if both operands are not true

not identical (!==, !) Returns true if both operands and their data types are not equal.

greater than (>) Returns true if the first operand has greater value than the second operand

greater than (>=, :>=) Returns true if the first operand has greater than or equal value than the second operand

lesser than (<) Returns true if the first operand has lesser value than the second operand

lesser than or equal to Returns true if the first operand has lesser than or (<=, :<=) equal value than the second operand

Bitwise Operators:
The bitwise operators act on the binary representation of their operands. Each operand is first turned into a binary representation of the value, and then the operation takes place. All the bitwise operators work on numbers as well as strings, but they vary in their treatment of string operands of different lengths. PHP bitwise operators are as follows:
~ (tilde): This bitwise operator negate, i.e., change 1s to 0s and 0s to 1s in the binary representation of the operands. Floating point values are converted to integers before the operation takes place. If the operand is a string, the resulting value is a string of same length as the original, with each character in the string negated.
Other bitwise operators are of binary type. They are:
& bitwise AND
| bitwise OR
^ (caret) bitwise XOR
<< left Shift
>> right Shift
The bitwise AND operator compares each corresponding bit in the binary representation of its operands. If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0.
Logical Operators:
Logical operators provide ways for building complex expressions. They treat their operands as Boolean value and return a Boolean value. Logical operators are:
&& (and)
|| (or)
xor
! (negation)

Leave a Reply