PHP Operators with Examples

PHP Operators

An Operators are used to perform operations on variables and values.

It means, we can describe operators that takes some values, and performs some operation on them and gives a result. From example, “1 + 2 = 3” in this expression ‘+’ is an operator.

PHP divides the operators in the following groups.

Arithmetic Operators:

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

List of Arithmetic Operators


Operator Description Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y.
* Multiplication $x * $y Product of $x and $y.
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y

Example:


Result:

14
6
40
2.5
2 


Assignment Operators:

The PHP assignment operators are used with nemeric 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.

List of Assignment Operators


Operator Description Example Is The Same As
= Assign $x = $y $x = $y
+= Add and assign $x += $y $x = $x + $y
-= Subtract and assign $x -= $y $x = $x - $y
*= Multiply and assign $x *= $y $x = $x * $y
/= Divide and assign quotient $x /= $y $x = $x / $y
%= Divide and assign modulus $x %= $y $x = $x % $y

Example:




Result:

10
50
30
125
5
10 


Comparison Operators:

The PHP comparison operators(!=, ==)are used to compare two calues (number or string).

These operators are used to compare two elements and outputs the result in boolean form.

List of Comparision Operators


Operator Name Example Result
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type
< Less than $x < $y True if $x is less than $y
> Greater than $x > $y True if $x is greater than $y
>= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y

Example:



Result:


bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false) 


Increment/Decrement 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.

List of Increment/Decrement Operators:

Operator Name Effect
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one

Example:



Result:


  11
  1110
  119
  910
  9	


Logical Operators:

The PHP logical operators are used to combine conditional statements.

List of Logical Operators

Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $$x or $y is true
! Not !$x True if $x is not true

Example:




String Operators:

PHP has two operators that are specially designed for strings.

They are concatenation (.) and concatenation assignment (.=).

The concatenation operator concatenates two strings. Eg: $str1.$str2.

The concatenation assignment operator(.=) assigns one string to another. Eg: $str1=$str2.

List of String Operators:

Operator Description Example Result
. Concatenation $str1 . $str2 Concatenation of $str1 and $str2
.= Concatenation assignment $str1 .= $str2 Appends the $str2 to the $str1

Example:




Result:



Welcome to : aimtocode!
Welcome to : aimtocode!



Array Operators:

The PHP array operators are used to compare arrays.

List of Array Operators:

Operator Name Example Result
+ Union $x + $y Union of $x and $y
== Equality $x == $y True if $x and $y have the same key/value pairs
=== Identity $x === $y True if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y True if $x is not equal to $y
<> Inequality $x <> $y True if $x is not equal to $y
!== Non-identity $x !== $y True if $x is not identical to $y

Example:



Result:


bool(false)
bool(false)
bool(true)
bool(true)
bool(true) 

Read Also: