Friday, March 20, 2009

Operators in php

In simple definition an operator is something that takes one or more values and generates another value. Operators can be of three types.

1)Unary Operator: Which operates on just one value. Like incremental operator (++) or negation operator(!).

2)Binary Operator: Which operates on two values. In php most of the operators are binary operators. Like +, -, * etc.

3)Ternary Operator: Which operators on three values. Example is ? : combination.
like, the expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Based on the operator functionality it can be categorized into following:
1) Arithmetic Operators: Some basic arithmetic operator falls within this category.
Examples are -(negation -$a), +(addition $a+$b), -(subtraction $a-$b), * (multiplication $a*$b), division ($a/$b), modulus ($a%$b) where a and b are both variable. Note that Remainder $a % $b is negative for negative $a.

2) Assignment Operators: The = (equal sign) is the basic assignment operator. Whenever you write $a=$b it mean value of right hand variable b(operand) will be assigned to the left hand variable a.

3) Bitwise Operators: There are several bitwise operator that operates bit by bit between operands. These operators are below and beside the operator is the result.

& (bitwise AND - bit is set if both operands are set)
| (bitwise OR - bit is set if any of the operand is set)
^ (bitwise XOR - bit is set if bit is set in either of the operands but not when both are set)
~ (NOT) - bit is set if it is not set in the operator and vice-versa.
<< (Shift Left) - &a<<&b means shifts the bit of $a by $b steps to the left. >> (Shift Right) - &a>>&b means shifts the bit of $a by $b steps to the right.

4) Comparison Operators: Comparison operators are for comparing between two values. They returns boolean value.
Examples are,
== ($a==$b returns true if $a is equal to $b)
=== ($a===$b returns true if $a is equal to $b and $a has the same datatype as of $b)
!= or <> ( $a!=$b or $a<>$b will return true if $a is not equal to $b)
!==($ a!== $b will return true if $a is not equal to $b, or they are not of the same type.
< ($a < $b will return true if $a is less than $b.) > ($a > $b will return true if $a is strictly greater than $b.)
<= ($a <= $b will return true if $a is less than or equal to $b.) >= ($a >= $b will return true if $a is greater than or equal to $b.

5) Error Control Operators: The @ (at sign) is error control operator in php. If @ is prepend to variables, function, include() calls, constants then any error messages that might be generated by that expression will be ignored.

If the track_errors feature is enabled- which can be enabled by setting TRACK_ERRORS, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error occurs.

6) Execution Operators: With php OS command can by executed by execution operator. If the commands are within backticks (` `) then PHP will attempt to execute the contents of the backticks as a shell command. Example is echo `ls -l`.

The backtick operator is disabled when safe mode (safe_mode =ON) is enabled or shell_exec() is disabled.

7) Incrementing/Decrementing Operators: Four incremental/decremental operators there.
++$a : Increments $a by one, then returns $a.
$a++ : Returns $a, then increments $a by one.
--$a : Decrements $a by one, then returns $a.
$a-- : Returns $a, then decrements $a by one.

8) Logical Operators:
AND : $a and $b becomes TRUE if both $a and $b are TRUE.
OR : $a or $b becomes TRUE if either $a or $b is TRUE.
XOR : $a xor $b becomes TRUE if either $a or $b is TRUE, but not both.
! : ! $a becomes TRUE if $a is not TRUE.
&& : $a && $b becomes TRUE if both $a and $b are TRUE.
|| : $a || $b becomes TRUE if either $a or $b is TRUE.

9) String Operators: One string type operation is concatenation(.) operator that concatenate it's left and right arguments. Another one is concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.

10) Array Operators: They work on array and examples are +,==,===, !=, <>,!== .

11) Type Operators: Example is, instanceof - which can be used to determine whether a variable is an instantiated object of a class.

No comments:

Post a Comment