Visit the Official PHP Website
Search this Site

PHP Programming

By James N Hitz

Common PHP Operators

There are so many PHP operators. A more thorough version with more detailed explanations is given here (click to open new window). Below we have extracted the list of the most common.

= AssignUsed for assigning values to a variable
+= Add and AsignAdds a value to an exisisting variable and assigns the answer to the same variable
-= Subtract and AssignSubtracts a value from an exisiting variable and assigns the answer to the same variable
*= Multiply and AssignMultiples an exisiting variable's value by another value and assigns the answer to the same variable
/= Divide and Assign Divides an existing variable by a value and assigns the answer to the same variable
Pause

I know the above operators (save for the first one) may be making your head spin. Rest easy. I'll explain one of them after which the others will be a breeze.

Supposing we have a variable $price which we assign the value 20:

$price = 20;

If we wanted to add a 5.00 storage charge to the price we would say that the new $price is the previous $price plus 5:

$price = $price + 5;
This can be "short-circuited" to read thus:
$price += 5;

This means the same as -=, /= and *= but with subtraction, division and multiplication respectively.

Have we cleared the air now? Hope so. Now, Play (remember the Pause?)

||OrTests if EITHER one of the operands evaluate to TRUE
&&AndTests if BOTH of the operands evaluate to TRUE
^ExponentRaises to the power of eg 10^2 (10 to the power 2)
==EqualityTests whether the operands are equal
!=Non-EqualityTests if the operands are NOT equal
< <= > >=less than or equal to, Greater Than and Greater than or Equal to respectively. Their application should be obvious
*Multiply
/Divide
%ModulusReturns the remainder (modulus) eg 10%3 = 1

If you want to have a better look at operators, click here (open's new window). Otherwise, I will assume you are okey and that things are fine. Come on {Shhh}no one will know you clicked{/Shhh}. Now... Back to Variables.

<< PHP Operators | What are Scalars? >>