An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).

Operators can be grouped according to the number of values they take. Unary operators take only one value, for example ! (the logical not operator) or ++ (the increment operator). Binary operators take two values, such as the familiar arithmetical operators + (plus) and - (minus), and the majority of PHP operators fall into this category. Finally, there is a single ternary operator, ? :, which takes three values; this is usually referred to simply as "the ternary operator" (although it could perhaps more properly be called the conditional operator).

Arithmetic Operators

These operators work on the numbers, float etc. It includes addition (+), multiplication(*), subtraction(-), division(/), modulus(%), and negation(-$a)
Negation is unary operator out of them. Rest all are binary operators.

Assignment Operators

Most basic assignment operator is = . It assigns the value of right variable to the left variable. Besides there are some combined operators for all types of binary arithmetic, array union and string manipulation(+=, -=, *=, /=, .=).

$a = 3; 
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
Assignment by reference

Assignment in PHP can also be done by reference by using assignment "$var = &$othervar;" syntax. Assignment by reference means that both variables end up pointing at the same data without any copy. As a result any change in value of one variable also changes the other variables.


$a = 3;
$b = &$a; // $b is a reference to $a

print "$a\n"; // prints 3
print "$b\n"; // prints 3

$a = 4; // change $a

print "$a\n"; // prints 4
print "$b\n"; // prints 4 as well, since $b is a reference to $a, which has been changed
Comparison Operators

These operators are meant for comparision of two values. Various operators to compare values are ==, ===, !=, <>, !==, <, >, <=, >=

==      Returns true if they are equal
===     Returns true if they are identical
!=      Returns true if they are not equal
<>      Returns true if they are not equal
!==     Returns true if they are not identical
<       Returns true if first value is smaller than second
<=      Returns true if first value is smaller or equal to  the second

Incrementing/Decrementing Operators

PHP also supports post increment and decrement like C.

++$a      Increments $a first by one  and then returns it
$a++      Increments after returning
--$a      Decrements before returning $a
$a--      Decrement after returning $a
Operators Precedence

Operators precedence decides the sequence in which a number of operators will operate on a tightly packed expression. Ex. (2*5+2) is 12 and not 14, because multiplication operator has higher precedence rank than addition. Lets have a look at the precedence table for the frequently used operators.

    * / %	Highest Precedence
 	+  -  .	
 	<  <=  >  >=	 
 	=== !==	 
 	OR	Lowest Precedence

You may not be familiar with === and !== operators. These are the newly defined operators available in later version of PHP. These are used to check the equality and inequality of two data which are not of the same type.

 $number = 3;
$text = 'three';

if ($number = = = $text) {
else {
print("Not the same");

To control the output, we have a number of control structures available in PHP. Lets have a look at some of the most important control structures.

Member Login

Member Login

Not a Member? Sign Up!

Login to comment

Be the first to comment on this topic


<<< Wanna review

Continue >>>