PHP Expressions and Operators

Operators are used to perform operations on variables and values. When we’re talking about operators we’re basically talking about “math”. By operator I mean things like multiplication and division signs.

As you might imagine there are tons of operators. Certainly enough to get lost in. The reality is, a lot of these you won’t use on a regular basis or they’ll make more sense if we cover them in a different lesson.

PHP Operators can be separated into the following categories:

  • PHP Arithmetic Operators
  • PHP Assignment Operators
  • PHP Incrementing / Decrementing Operators
  • PHP Comparison Operators
  • PHP Logical Operators
  • Bitwise Operators
  • PHP Array Operators
  • PHP String Operators

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).

Operator Precedence

The precedence of an operator specifies how “tightly” it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication (“*”) operator has a higher precedence than the addition (“+”) operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.

When operators have equal precedence their associativity decides how the operators are grouped. For example “-” is left-associative, so 1 – 2 – 3 is grouped as (1 – 2) – 3 and evaluates to -4. “=” on the other hand is right-associative, so $a = $b = $c is grouped as $a = ($b = $c).

Operators of equal precedence that are non-associative cannot be used next to each other, for example 1 < 2 > 1 is illegal in PHP. The expression 1 <= 1 == 1 on the other hand is legal, because the == operator has lesser precedence than the <= operator.

Although parentheses are not strictly needed, the readability of the code can often be increased by explicitly grouping rather than by using the implicit predominance and association of the operator.

non-associativeclone new
right++ ~ (int) (float) (string) (array) (object) (bool) @
left* / %
left+ .
left<< >>
non-associative< <= > >=
non-associative== != === !== <> <=>
left? :
right= += -= *= **= /= .= %= &= |= ^= <<= >>=

The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides grouping.

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

a + bAddition5 + 49
a – bSubtraction10 – 19
a * bMultiplication3 * 39
a / bDivision18 / 29
a % bModulus20 % 11
8 % 2
a . bConcatenation“Hello”.”World”Hello World

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the assignment expression on the right.

AssignmentSimilar to …Description
a = ba = bValue of expression on right is assigned to left operand. Here value of ‘b’ is assigned to operand ‘a’.
a += ba = a + bAddition
a -= ba = a – bSubtraction
a *= ba = a * bMultiplication
a /= ba = a / bDivision
a %= ba = a % bModulus
a .= ba = a . bConcatenation

PHP Incrementing / Decrementing Operators

The PHP increment operators are used to increment a variable’s value.

The PHP decrement operators are used to decrement a variable’s value.

++aPre-incrementIncrements a by 1 and returns a
a++Post-incrementReturns a, then increments by 1
–aPre-decrementDecrements by 1 and returns a
a–Post-decrementReturns a, then decrements by 1

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.

a == bEqualReturns true if a is equal to b5 == 4 returns false
a === bIdenticalReturns true if a and b are equal and they are of same type9 === “9” returns false
a != b
a <> b
Not equalReturns true if a is not equal to b3 != 2
3 <> 2
returns true
a !== bNot IdenticalReturns true if a is not equal to b or they are not of same type9 !== “9” returns true
a > bGreater thanReturns true if a is greater than b9 > 3 returns true
a < bLess thanReturns true if a is less than b9 < 3 returns false
a >= bGreater than or equal toReturns true if a is greater than or equal to b9 >= 3 returns true
a <= bLess than or equal toReturns true if a is less than or equal to b9 <= 3 returns false

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

a and b
a && b
AndReturns true if both a and b are truea = 3 b = 9
(a < 5 and b > 4) returns true
(a < 5 && b > 10) returns false
a or b
a || b
IdenticalReturns true if either or both a and b are truea = 9 b = 5
(a > 3 or b == 8) returns true
(a == 5 || b > 9) returns false
a xor bXorReturns true if either a or b is true, but not botha = 8 b = 5
(a == 8 xor b == 5) returns false
!aNotReturns true if a is not truea = 9 b = 7
!(a < b) returns true

Bitwise Operator In PHP

Another kind of operators are bitwise operators. They compare bits of variables with each other and allow evaluation and manipulation of specific bits within an integer.

The detailed illustrations of them are out of these lessons and just we introduce them and their names:

$x & $yANDAnd together each bit contained in $x and $y
$x | $yOROr together each bit contained $x and $y
$x ^ $yXORExclusive – or together each bit contained in $x and $y
~$xNOTNegate each bit in $x
$x << $yShift Left$x will receive the value of $y shifted left tow bits
$x >> $yShift Right$x will receive the value of $y shifted right two bits

PHP Array Operators

The PHP array operators are used to compare arrays.

a + bUnionUnion of a and b
a == bEqualityReturns true if a and b have same key / value pairs
a === bIdentityReturns true if a and b have same key / value pairs in the same order and of the same type
a != b
a <> b
InequalityReturns true if a is not equal to b
a !== bNon-identityReturns true if a is not identical to b

String operators in PHP :

PHP has two operators that are specially designed for strings.

Operator Name Example Result. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2.=Concatenation assignment$txt1 .= $txt2Appends $txt2 to $txt1

$x = “vikram”. “y”Concatenation$x is assigned the string vikramy
$x .= “edmasters”;Concatenation-assignment$x equals its current value concatenated with “edmasters”

Add a Comment

Your email address will not be published. Required fields are marked *


My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib


Request a Quote