PHP – Operators

What are Operators in PHP?

As the name define, Operators are used for doing operations on PHP variables.

PHP devided the operators in the following categories:

  • Arithmetic operators
  • Comparison operators
  • Assignment operators
  • Logical operators
  • Increment/Decrement operators
  • String operators
  • Array operators

Arithmetic operators in PHP

Arithmetic operators are used with numeric values to perform arithmetical operations like addition, subtraction, multiplication etc.

Operator Name Example
+ Addition $a + $b result will be sum of $a and $b
Subtraction $a + $b result will be difference of $a and $b
* Multiplication $a + $b result will be multiplication of $a and $b
/ Division $a + $b result will be quotient of $a and $b
% Modulus $a + $b result will be remainder of $a and $b
++ Increment operator $a++ result will be  $a+1
Decrement operator $a– result will be  $a-1

Comparison operators in PHP

The comparison operators in PHP are used to compare the values of two variables.

Operator Description Example
== Equal $a == $b Returns  true if $a is equal to $b
!= and <> Not equal $a != $b Returns  true if $a is not equal to $b
> Greater than $a == $b Returns  true if $a is greater than $b
< Less than $a == $b Returns  true if $a is less than $b
>= Greater than or equal to $a == $b Returns  true if $a is greater than or equal to $b
<= Less than or equal to $a == $b Returns  true if $a is less than or equal to $b

Assignment operators in PHP

The assignment operators in PHP are used to assign a value in a PHP variable.

Operator Description Example
= Simple assignment operator $a = $b + $c will assign value of $b + $c into $a
+= Add AND assignment operator $a += $b  will assign value of $a + $b into $a
-= Subtract AND assignment operator $a -= $b  will assign value of $a – $b into $a
*= Multiply AND assignment operator $a *= $b  will assign value of $a * $b into $a
/= Divide AND assignment operator $a /= $b  will assign value of $a / $b into $a
%= Modulus AND assignment operator $a %= $b  will assign value of $a % $b into $a

Logical operators in PHP

The logical operators in PHP are used to combine the conditional statements.

Operator Name Example
and AND operator “$a and $b” Returns true if both $a and $b are true
or OR Operator “$a and $b” Returns true if both $a or $b is true
&& AND operator “$a && $b” Returns true if both $a and $b are true
|| OR Operator “$a || $b” Returns true if both $a or $b is true
! NOT Operator !$a Returns true if $a is not true

Increment/Decrement operators in PHP

The Increment Operators are used in PHP to increase a variable’s value by 1.

The decrement operators are used in PHP to decrease a variable’s value by 1.

Operator Name Example
++$a Pre-increment First increments $a by one, and then returns $a.

(1+$a)

$a++ Post-increment First returns $a, then increments $a by one.

($a+1)

–$a Pre-decrement First decrements $a by one, and then returns $a.

(1-$a)

$a– Post-decrement First returns $a, and then decrements $a by one.

($a-1)

String operators in PHP

String operators are used in PHP to conbine two variables.

Operator Description Example
. Concatenation $str1 . $str2

Concatenation of $str1 and $str2

.= Concatenation assignment $str1 .= $str2

Appends $str1 to $str2

Array operators in PHP

Array operators are used in PHP to compare arrays.

Operator Name Example
+ Union $a + $b

Returns Union of $x and $y

== Equality Returns true if $a and $b have the same key pairs
=== Identity Returns true if $a and $b have the same key pairs in the same order and of the same types
!= Inequality Returns true if $a is not equal to $b
<> Inequality Returns true if $a is not equal to $b
!== Non-identity Returns true if $a is not identical to $b

Leave a Reply

Your email address will not be published.