## PHP Operators

Operators are used to performing operations on some values.

PHP Operators can be categorized in following forms:

1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Increment/Decrement operators
5. Logical operators
6. String operators
7. Array operators
8. Conditional or Ternary Operators

Arithmetic operators:

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here's a complete list of PHP's arithmetic operators.

Operator Description Example Explanation
`+` Addition `\$x + \$y` Addition of two numbers
`-` Subtraction `\$x - \$y` Subtraction of two numbers
`*` Multiplication `\$x * \$y` Multiplication of two numbers
`/` Division `\$x / \$y` Quotient of \$x and \$y
`%` Modulus `\$x % \$y` Remainder of \$x divided by \$y

Example:

``````<?php
\$x = 16; // Variable 1
\$y = 5; // Variable 2

// Some arithmetic operations on these two variables
echo (\$x + \$y), "\n";
echo(\$x - \$y), "\n";
echo(\$x * \$y), "\n";
echo(\$x / \$y), "\n";
echo(\$x % \$y), "\n";
?>

Output :

21
11
80
3.2
1``````

Assignment Operators:

The assignment operators are used to assign value to different variables. The basic assignment operator is "=".

Assignment Same as... Description
x = y x = y The left operand gets set to the value of the expression on the right
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus

Example :

``````<?php
// Simple assign operator
\$y = 25;
echo \$y, "\n";

\$y = 25;
\$y += 75;
echo \$y, "\n";

// Subtract then assign operator
\$y = 60;
\$y -= 10;
echo \$y, "\n";

// Multiply then assign operator
\$y = 10;
\$y *= 5;
echo \$y, "\n";

// Divide then assign(quotient) operator
\$y = 50;
\$y /= 5;
echo \$y, "\n";

// Divide then assign(remainder) operator
\$y = 51;
\$y %= 5;
echo \$y;
?>

Output :
25
100
50
50
10
1``````

Comparison operators:

The PHP comparison operators are used to compare two values (number or string)

Operator Name Example Explanation
== Equal \$a == \$b Return TRUE if \$a is equal to \$b
=== Identical \$a === \$b Return TRUE if \$a is equal to \$b, and they are of same data type
!== Not identical \$a !== \$b Return TRUE if \$a is not equal to \$b, and they are not of same data type
!= Not equal \$a != \$b Return TRUE if \$a is not equal to \$b
<> Not equal \$a <> \$b Return TRUE if \$a is not equal to \$b
< Less than \$a < \$b Return TRUE if \$a is less than \$b
> Greater than \$a > \$b Return TRUE if \$a is greater than \$b
<= Less than or equal to \$a <= \$b Return TRUE if \$a is less than or equal \$b
>= Greater than or equal to \$a >= \$b Return TRUE if \$a is greater than or equal \$b
<=> Spaceship \$a <=>\$b Return -1 if \$a is less than \$b
Return 0 if \$a is equal \$b
Return 1 if \$a is greater than \$b

Example:

``````<?php
\$a = 100;
\$b = 50;
\$c = "100";

// Here var_dump function has been used to
// display structured information. We will learn
// articles.
var_dump(\$a == \$c) + "\n";
var_dump(\$a != \$b) + "\n";
var_dump(\$a <> \$b) + "\n";
var_dump(\$a === \$c) + "\n";
var_dump(\$a !== \$c) + "\n";
var_dump(\$a < \$b) + "\n";
var_dump(\$a > \$b) + "\n";
var_dump(\$a <= \$b) + "\n";
var_dump(\$a >= \$b);
?>

Output:
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)``````

Increment / Decrement operators:

The increment/decrement operators are used to increment/decrement a variable's value.

Operator Name Description
++\$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:

``````<?php
\$x = 5;
echo ++\$x; // Outputs: 6
echo \$x;   // Outputs: 6

\$x = 5;
echo \$x++; // Outputs: 5
echo \$x;   // Outputs: 6

\$x = 5;
echo --\$x; // Outputs: 4
echo \$x;   // Outputs: 4

\$x = 5;
echo \$x--; // Outputs: 5
echo \$x;   // Outputs: 4
?>``````

Logical Operators:

The PHP logical operators are used to combine conditional statements.

Operator Description Example
and Called Logical AND operator. If both the operands are true then condition becomes true. (A and B) is true.
or Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. (A or B) is true.
&& Called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is true.
|| Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is false.

String Operators:

The string operators are used to perform the operation on strings.

Operator Name Example Explanation
. Concatenation \$a . \$b Concatenate both \$a and \$b
.= Concatenation and Assignment \$a .= \$b First concatenate \$a and \$b, then assign the concatenated string to \$a, e.g. \$a = \$a . \$b

Array Operators:

The array operators are used in case of array. Basically, these operators are used to compare the values of arrays.

Operator Name Example Explanation
+ Union \$a + \$y Union of \$a and \$b
== Equality \$a == \$b Return TRUE if \$a and \$b have same key/value pair
!= Inequality \$a != \$b Return TRUE if \$a is not equal to \$b
=== Identity \$a === \$b Return TRUE if \$a and \$b have same key/value pair of same type in same order
!== Non-Identity \$a !== \$b Return TRUE if \$a is not identical to \$b
<> Inequality \$a <> \$b Return TRUE if \$a is not equal to \$b

Conditional or Ternary Operators:

The PHP Conditional or Ternary Operators are used to set a value depending on conditions:

Operator Name Example Result
?: Ternary \$x = expr1 ? expr2 : expr3 Returns the value of \$x.
The value of \$x is expr2 if expr1 = TRUE.
The value of \$x is expr3 if expr1 = FALSE
?? Null coalescing \$x = expr1 ?? expr2 Returns the value of \$x.
The value of \$x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of \$x is expr2.
Introduced in PHP 7