PHP Operator Types

An operator basically used to perform operation on some values

The PHP Operator

In general, an operator is a sign that performs some task on different value and variable in order to feedback the desired result. Like any other programming languages, PHP is built on different types of operator. That is basically used for performing an operation on some values or even variables.

For example, 5+4=9. You know it is a simple arithmetical additional operation. Yes, just like any other programming languages, while coding in PHP you will often use a different kind of operators including the arithmetical operator.

Anyway, in the above example, “5” and “4” are two integer values and “+” is an arithmetical operator that performs an additional operation between the two values. Finally, the operation produces an ultimate result “9“.

In PHP we will learn about different kind of operators. The most used operators are listed below:

  • Arithmetic operators / Simple mathematical operators
  • Logical Operators
  • Conditional Operators / Ternary Operators
  • Comparison Operator
  • Assignment Operators
  • String Operators
  • Array Operators
  • Increment/Decrement Operators
  • Spaceship Operators

Now we will learn those operators in details:

Arithmetic Operators

The arithmetic operator is the most basic mathematical operator that performs the basic mathematical operations like substruction, addition, division, multiplication etc. See the below list of arithmetic operator supported by PHP

Operator name Operator sign Syntax workflow
Addition + $a + $b
Substruction $a – $b
Multiplication * $a * $b
Division / $a / $b
Exponentiation ** $a ** $b
Modulus % $a % $b

Example:

<?php 
// variable 1
$a = 35;
// variable 2
$b = 3; 
//now we will play artihmetical operation on these two variable
echo ($a + $b), "\n";
echo($a - $b), "\n";
echo($a * $b), "\n";
echo($a / $b), "\n";
echo($a % $b), "\n";
?>

Output:

38
32
105
11.666666666667
2