PHP Operators

Operators are used to perform operations on PHP variables and simple values.

In PHP there are total 7 types of operators, they are:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Increment/Decrement Operators
  5. Logical Operators
  6. String Operators
  7. Array Operators

There are a few additional operators as well like, Type operator, Bitwise operator, Execution operators etc.

Based on how these operators are used, they are categorised into 3 categories:

  1. Unary Operators: Works on a single operand(variable or value).
  2. Binary Operators: Works on two operands(variables or values).
  3. Ternary Operators: Works on three operands.

PHP Arithmetic Operators

These operators are used to perform basic arithmetic operations like addition, multiplication, division, etc.

NameOperatorWhat does it do?Example
Addition+It is used to perform normal addition.$a + $b
Subtraction-It is used to perform normal subtraction.$a - $b
Multiplication*It is used to perform multiplication.$a * $b
Division/It is used to perform division.$a / $b
Exponent**It returns the first operand raised to the power the second operand. $a ** $b = $a$b$a ** $b
Modulus(or, Remainder)%It returns the remainder of first operand divided by the second operand$a % $b

PHP Assignment Operators

Assignment operators are used to assign values to variables, either as it is or after performing some arithmetic operation on it. The most basic assignment operator is equal to=.

OperatorUsage
=$a = $b, will save the value of variable $b to the variable $a
+-$a += $b is same as $a + $b
-=$a -= $b is same as $a - $b
*=$a *= $b is same as $a * $b
/=$a /= $b is same as $a / $b
%=$a %= $b is same as $a % $b

So basically, the assignment operator provides us with shorthand techniques to perform arithmetic operations.


PHP Comparison Operators

As the name suggest, these are used to compare two values.

NameOperatorWhat does it do?Example
Equal==It returns true if left operand is equal to the right operand.$a == $b
Identical===It returns true if left operand is equal to the right operand and they are of the same type.$a === $b
Not Equal!=It returns true if left operand is not equal to the right operand.$a != $b
Not Identical!==It returns true if left operand is not equal to the right operand, and they are of different type as well.$a !== $b
Greater than>It returns true if left operand is greater than the right operand.$a > $b
Less than<It returns true if left operand is less than the right operand.$a < $b
Greater than or equal to>=It returns true if left operand is greater than or equal to the right operand.$a >= $b
Less than or equal to<=It returns true if left operand is less than or equal to the right operand.$a <= $b

PHP Increment/Decrement Operators

These operators are unary operators, i.e they require only one operand.

OperatorUsage
++$aPre Increment, It will first increment the operand by 1(add one to it) and then use it or return it.
$a++Post Increment, It will first return the operand and then increment the operand by 1.
--$bPre Decrement, It will first decrement the operand by 1(subtract one from it) and then use it or return it.
$b--Post Decrement, It will first return the operand and then decrement the operand by 1.

These operators are very useful and handy when use loops or when we have simply increment any value by one in our program/script.


PHP Logical Operators

Logical operators are generally used when any action depends on two or more conditions.

NameOperatorWhat does it do?Example
Andand or &&It returns true if both the operands(or expressions) returns true.$a && $b
Oror or ||It returns true if any one out of the two operands(or expressions) returns true, or both return true.$a || $b
XorxorIt returns true if any one out of the two operands(or expressions) returns true, but not when both return true.$a xor $b
Not!This is a unary operator. It returns true, if the operand(or expression) returns false.!$a

PHP String Operators

String operators are used to perform operations on string. There are only two string operators, generally PHP built-in functions are used to perform various operations on strings, we will learn about them in coming tutorials.

NameOperatorWhat does it do?Example
Concatenation. (a dot)It is used to concatenate(join together) two strings.$a.$b
Concatenation Assignment.=It is used to append one string to another.$a .= $b

Here we have a simple example to demonstrate the usage of both the string operators.

<?php
    $a = "study";
    $b = "tonight";
    // concatenating $a and $b
    echo $a.$b;
    
    // appending $b to $a
    $a .= $b
    echo $a;
?>

studytonight studytonight


PHP Array Operators

These operators are used to compare arrays.

NameOperatorWhat does it do?Example
Equal==It returns true if both the arrays have same key/value pairs.$a == $b
Identical===It returns true if both the arrays have same key/value pairs, in same order and of same type.$a === $b
Not Equal!=It returns true if both the arrays are not same.$a != $b
Not Identical!==It returns true if both the arrays are not identical, based on type of value etc.$a !== $b
Union(Join)+It joins the arrays together$a + $b