PHP Operators

PHP has different types of operators for different operations. They are as follows:

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations.

NameOperatorExample
Addition+$x + $y
Subtraction-$x - $y
Multiplication*$x * $y
Division/$x / $y
Modulus%$x % $y
Exponentiation**$x ** $y

2. Assignment Operators

These operators are used to assign values to variables.

NameEvaluated as
=a = b
+=a = a + b
-=a = a - b
*=a = a * b
/=a = a / b
%=a = a % b

3. Comparison Operators

These operators are used to compare two values.

NameOperatorExample
Equal==$x == $y
Identical===$x === $y
Not equal!=$x != $y
Not equal<>$x <> $y
Not Identical!==$x !== $y
Greater than>$x > $y
Less than<$x < $y
Greater than or equal to>=$x >= $y
Less than or equal to<=$x <= $y
Spaceship<=>$x <=> $y

4. PHP Increment/Decrement Operators

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

NameOperator
Pre-Increment++$x
Post-Increment$x++
Pre-decrement--$x
Post-decrement$x--

5. PHP Logical Operators

These are the logical operators that combine conditional statements.

NameOperatorExample
Andand$x and $y
Oror$x or $y
Xorxor$x xor $y
And&&$x && $y
Or||$x || $y
Not!!$x

6. PHP String Operators

PHP has these two operators designed for strings.

NameOperatorExample
Concatenation.$text1 . $text2
Concatenation Assignment.=$text1 .= $text2

7. PHP Array Operators

These operators are used to compare arrays.

NameOperatorExample
Union+$x + $y
Equality==$x == $y
Identity===$x === $y
Inequality!=$x != $y
Inequality<>$x <> $y
Non-Identity!==$x !== $y

8. PHP Conditional Operators

These operators assign values to operands based on the outcome of a certain condition.

NameOperatorExample
Ternary?:$x = exp1 ? exp2 : exp3
Null Coalescing??$x = exp1 ?? exp2