Welcome! If you like our Blog so please Follow this blog and also +1 this blog here---->

Wednesday 15 August 2012

Pin It

PHP Tutorial 6: PHP Operators


The assignment operator = is used to assign values to variables in PHP.
The arithmetic operator + is used to add values together.

Arithmetic Operators

The table below lists the arithmetic operators in PHP:
OperatorNameDescriptionExampleResult
x + yAdditionSum of x and y2 + 24
x - ySubtractionDifference of x and y5 - 23
x * yMultiplicationProduct of x and y5 * 210
x / yDivisionQuotient of x and y15 / 53
x % yModulusRemainder of x divided by y5 % 2
10 % 8
10 % 2
1
2
0
- xNegationOpposite of x- 2 
a . bConcatenationConcatenate two strings"Hi" . "Ha"HiHa

Assignment Operators

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the expression on the right. That is, the value of "$x = 5" is 5.
AssignmentSame as...Description
x = yx = yThe left operand gets set to the value of the expression on the right
x += yx = x + yAddition
x -= yx = x - ySubtraction
x *= yx = x * yMultiplication
x /= yx = x / yDivision
x %= yx = x % yModulus
a .= ba = a . bConcatenate two strings

Incrementing/Decrementing Operators

OperatorNameDescription
++ xPre-incrementIncrements x by one, then returns x
x ++Post-incrementReturns x, then increments x by one
-- xPre-decrementDecrements x by one, then returns x
x --Post-decrementReturns x, then decrements x by one

Comparison Operators

Comparison operators allows you to compare two values:
OperatorNameDescriptionExample
x == yEqualTrue if x is equal to y5==8 returns false
x === yIdenticalTrue if x is equal to y, and they are of same type5==="5" returns false
x != yNot equalTrue if x is not equal to y5!=8 returns true
x <> yNot equalTrue if x is not equal to y5<>8 returns true
x !== yNot identicalTrue if x is not equal to y, or they are not of same type5!=="5" returns true
x > yGreater thanTrue if x is greater than y5>8 returns false
x < yLess thanTrue if x is less than y5<8 returns true
x >= yGreater than or equal toTrue if x is greater than or equal to y5>=8 returns false
x <= yLess than or equal toTrue if x is less than or equal to y5<=8 returns true

Logical Operators

OperatorNameDescriptionExample
x and yAndTrue if both x and y are truex=6
y=3
(x < 10 and y > 1) returns true
x or yOrTrue if either or both x and y are truex=6
y=3
(x==6 or y==5) returns true
x xor yXorTrue if either x or y is true, but not bothx=6
y=3
(x==6 xor y==3) returns false
x && yAndTrue if both x and y are truex=6
y=3
(x < 10 && y > 1) returns true
x || yOrTrue if either or both x and y are truex=6
y=3
(x==5 || y==5) returns false
! xNotTrue if x is not truex=6
y=3
!(x==y) returns true

Array Operators

OperatorNameDescription
x + yUnionUnion of x and y
x == yEqualityTrue if x and y have the same key/value pairs
x === yIdentityTrue if x and y have the same key/value pairs in the same order and of the same types
x != yInequalityTrue if x is not equal to y
x <> yInequalityTrue if x is not equal to y
x !== yNon-identityTrue if x is not identical to y