Operators in PHP
Operators in PHP or any other programming languages are special symbols that tell the PHP interpreter to perform certain actions.
In other words, operators are symbols that are used when we want to perform an operation on one or more values or variables.
For example, the addition (+) symbol is an operator that tells the PHP interpreter to add two variables or values, while the greater than (>) symbol is an operator that tells the interpreter to compare two values.
There are more than 50 operators in PHP that cover arithmetic operations, logical comparisons, bitwise calculations, and more. These operators allow you to perform a variety of tasks efficiently when working with variables and data.
Expressions in PHP
When we use an operator with one or more values or variables, they form an expression. In PHP or other programming languages, an expression is something that has a value. It can be either a simple value, a single operand to express a value, or a set of operands which are combined together using operators.
The operands of an expression can be constants, variables, or other sub-expressions. In general, an operand is simple anything that an operator works on. Let’s take an example to understand the difference between operands, operators, and sub-expressions.
Example:
$var = (20 + $pi) * 2;
In the above simple expressions,
- Constants, such as 20, $pi, and 2 are the operands.
- The signs minus ( – ) and multiplication (*) are operators.
- The (24 + $pi) expresses a sub-expression.
- (20 + $pi) * 2; is an expression.
Example 2:
$var2 = 10;Here, $var2 is a name of variable and 10 is the constant value assigned to it. In this example, 10 is an expression.
Example 3:
$var3 = $var4;In this example, $var4 is an expression.
Example 4:
$var5 = 10 + 5 + 20;Here, 10 + 5 + 20 is an expression which consists of two operators and three operands.
Types of PHP Operators based on Operands
We can categorize operators in PHP based on the number of operands they work on. Based on the number of operands, PHP mainly supports three types of operators. They are as:
- Unary operators
- Binary operators
- Ternary operator
1. Unary Operators
Unary operators are those which operate on a single operand. They generally perform operations like increment or decrement a value, negating a value, or checking logical states.
Some examples of unary operators are:
- Increment (
++): Increases the value of a variable by one. - Decrement (
--): Decreases the value of a variable by one. - Logical NOT (
!): Inverts the boolean value of an expression. - Negation (
-): Returns the negative of a number.
Example 5:
<?php
$x = 5;
echo ++$x; // Pre-increment: Outputs 6
echo -$x; // Negation: Outputs -6
?>2. Binary Operators
Binary operators are those which operate on two operands. These operators are the most commonly used operators in PHP and cover a wide range of operations, such as arithmetic, logical comparisons, and bitwise calculations.
Examples of Binary Operators:
- Arithmetic (
+, -, *, /, %): Perform mathematical operations between two values. - Logical (
&&, ||): Perform logical operations between two boolean values. - Comparison (
==, !=, >, <): Compare two values. - Bitwise (
&, |, ^): Perform bit-level operations on binary representations of two integers.
Example 6:
<?php
// Program to perform the addition of two values and comparison between them.
$a = 10;
$b = 5;
echo $a + $b;
echo $a > $b;
?>Output:
15
true
3. Ternary Operator
The ternary operator is a special type of operator in PHP that works with three operands. It is commonly used as a shorthand for the if-else conditional statement. An example of a ternary operator is given below:
Example 7:
<?php
$age = 18;
echo ($age >= 18) ? "Adult" : "Minor";
?>Output:
Adult
Types of Operators in PHP
PHP provides various types of operators, divided into different categories as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Increment or decrement operators
- Conditional operator
- String operators
- Array operators
- Bitwise operators
- Null Coalescing Operator
- Spaceship Operator
Arithmetic Operators in PHP
Arithmetic operators are those operators that are used in mathematical expressions to perform the basic operations such as addition, subtraction, multiplication, and division.
PHP supports six types of arithmetic operators:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponentiation (**)
Addition (+):
This operator adds two or more operands and returns the sum. It is used to perform a mathematical addition between numeric values or variables in PHP.
Example 8:
<?php
$x = 10;
$y = 20;
$sum = $x + $y;
echo $sum;
?>Output:
30
In this example, we have assigned integer type values 10 and 20 to the variables, $x and $y, respectively. The value 10 of the $x is added to the value 20 of the variable $y. After that, we have assigned the resultant value 3 to the variable named $sum and displayed it on the console or browser.
Subtraction (-):
This operator subtracts one operand from another operand and returns the difference. It is used to perform a mathematical subtraction between numeric values or variables in PHP.
Example 9:
<?php
$x = 20;
$y = 10;
$sub = $x - $y;
echo $sub;
?>
Output:
10
In this example, we have calculated the subtraction operation. The value 20 of the $x variable is subtracted from the value 10 of $y variable. After that, we have assigned the resultant value to a variable $sub and display it.
Multiplication (*):
This operator multiplies one or more operands and returns the product. You can use it to perform a mathematical multiplication between numeric values or variables in PHP.
Example 10:
<?php
$x = 5;
$y = 20;
$multiply = $x * $y;
echo $multiply;
?>
Output:
100
In this example, we have performed a multiplication operation between two numeric values. We have multiplied the value 5 of the $x variable with the value 20 of the $y variable and stored the resultant into a variable named $multiply.
Division (/):
This operator divides one operand by another and returns the quotient. With this operator, you can perform a mathematical division operation between numeric values or variables in PHP programming. Let’s take an example in which we will perform division operation.
Example 11:
<?php
$x = 100;
$y = 20;
$div = $x / $y;
echo $div;
?>
Output:
5
Modulus (%):
The modulus operator divides the value of the left-hand operand by that of the right-hand operand and returns the remainder of a division operation. You can use the modulus operator (%) to know a number is divisible by another. For example, if the result of modulus is zero, it means that the number is perfectly divisible.
Example 12:
<?php
$x = 124;
$y = 5;
$mod = $x % $y;
echo $mod;
?>
Output:
4
Exponentiation (**):
The exponentiation operator (**) raises a number to the power of another number and returns the result.
Example 13:
<?php
$x = 16;
$y = 3;
$expo = $x ** $y;
echo $expo;
?>
Output:
4096
In this example, the number 16 is raised to the third power, resulting in 4096.
Complete Arithmetic Operators in Table Form
Here, we have listed all the arithmetic operators in a table that you should keep in mind the basic definition.
| Operator | Operation | Example | Description |
| + | Addition | $x + $y | Sum of operands |
| – | Subtraction | $x – $y | Difference of operands |
| * | Multiplication | $x * $y | Product of operands |
| / | Division | $x / $y | Quotient of operands |
| % | Modulus | $x % $y | Remainder of operands |
| ** | Exponential | $x ** $y | Raises a number to the power |
Example 14:
<?php
$x = 20;
$y = 4;
echo "$x + $y = " .($x + $y);
echo "\n";
echo "$x - $y = " .($x - $y);
echo "\n";
echo "$x * $y = " .($x * $y);
echo "\n";
echo "$x / $y = " .($x / $y);
echo "\n";
echo "$x % $y = " .($x % $y);
echo "\n";
echo "$x ** $y = " .($x ** $y);
?>
Output:
20 + 4 = 24
20 - 4 = 16
20 * 4 = 80
20 / 4 = 5
20 % 4 = 0
20 ** 4 = 160000
In this tutorial, you have learned about expressions and operators in PHP with the help of various examples. In addition to them, you have performed various arithmetic operations, such as addition, subtraction, multiplication, division, modulus, and exponential using arithmetic operators.




