PHP operator is a character or symbol that performs a special operation within the PHP code. For example, If you use the equals sign ( = ) to assign a value to a variable, you are using an assignment operator. If you add two numbers together using a plus sign ( + ), you are using an arithmetic operator.
The following are the lists of different operators used in PHP.
- Arithmetic Operators
- Assignment Operators
- Logical or Relational Operators
- Comparison Operators
- Conditional or Ternary Operators
- Increment/Decrement Operators
- Array Operators
- String Operators
- Spaceship Operators
Arithmetic Operators in PHP:
The arithmetic operators are used to perform simple arithmetic operations such as addition, subtraction, multiplication, division, etc. The list of arithmetic operators are as follows:
Operator | Name | Example | Description |
---|---|---|---|
+ | Addition | $a + $b | Sum of $a and $b |
- | Subtraction | $a - $b | Difference of $a and $b |
* | Multiplication | $a * $b | Product of $a and $b |
/ | Division | $a / $b | Quotient of $a and $b |
% | Modulus | $a % $b | Remainder of $a divided by $b |
Example:
<?php
$a = 15;
$b = 5;
echo($a + $b), "\n";  // 0utputs: 20
echo($a - $b), "\n";  // 0utputs: 10
echo($a * $b), "\n";  // 0utputs: 75
echo($a / $b), "\n";  // 0utputs: 3
echo($a % $b), "\n";  // 0utputs: 0
?>
Assignment Operators in PHP:
The assignment operators assign the values to a variable. The commonly used assignment operator is =, which assigns the variable on the left the value on the right side of the operand. The list of assignment operators are as follows:
Operator | Name | Example | Same as |
---|---|---|---|
= | Assign | $a = $b | $a = $b |
+= | Add and assign | $a += $b | $a = $a + $b |
-= | Subtract and assign | $a -= $b | $a = $a - $b |
*= | Multiply and assign | $a *= $b | $a = $a * $b |
/= | Divide and assign quotient | $a /= $b | $a = $a / $b |
%= | Divide and assign modulus | $a %= $b | $a = $a % $b |
Example:
<?php
$a = 15;
echo $a, "\n";  //output: 15
$b = 3;
$b += 10;
echo $b, "\n";  //output: 13
$c = 25;
$c -= 10;
echo $c, "\n";  //output: 15
$d = 2;
$d *= 4;
echo $d, "\n";  //output: 8
$e = 100;
$e /= 10;
echo $e, "\n";  //output: 10
$f = 10;
$f %= 4;
echo $f;  //output: 2
?>
Comparison Operators in PHP:
The list of comparison operators are as follows:
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | $a == $b | True if $a is equal to $b |
=== | Identical | $a === $b | True if $a is equal to $b, and they are of the same type |
!= | Not equal | $a != $b | True if $a is not equal to $b |
<> | Not equal | $a <> $b | True if $a is not equal to $b |
!== | Not identical | $a !== $b | True if $a is not equal to $b, or they are not of the same type |
< | Less than | $a < $b | True if $a is less than $b |
> | Greater than | $a > $b | True if $a is greater than $b |
>= | Greater than or equal to | $a >= $b | True if $a is greater than or equal to $b |
<= | Less than or equal to | $a <= $b | True if $a is less than or equal to $b |
Example:
<?php
$a = 10;
$b = 25;
$c = "10";
var_dump($a == $c) + "\n";  //output: bool(true)
var_dump($a === $c) + "\n";  //output: bool(false)
var_dump($a != $b) + "\n";  //output: bool(true)
var_dump($a !== $c) + "\n";  //output: bool(true)
var_dump($a < $b) + "\n";  //output: bool(true)
var_dump($a > $b) + "\n";  //output: bool(false)
var_dump($a <= $b) + "\n";  //output: bool(true)
var_dump($a >= $b) + "\n";  //output: bool(false)
?>
Logical or Relational Operators in PHP:
The logical operators combine conditionals and are mostly used to check multiple conditions at once. For example, PHP “or” operator checks whether at least one value is true out of two values. If you need to check whether both of them are true, you should use PHP and the operator.
Operator | Name | Example | Result |
---|---|---|---|
and | And | $a and $b | True if both $a and $b are true |
or | Or | $a or $b | True if either $a or $b is true |
xor | Xor | $a xor $b | True if either $a or $b is true, but not both |
&& | And | $a && $b | True if both $a and $b are true |
|| | Or | $a || $b | True if either $a or $b is true |
! | Not | !$a | True if $a is not true |
> | Greater than | $a > $b | True if $a is greater than $b |
Example:
<?php
$x = 10;
$y = 13;
$z = false;
if ($x == 10 and $y == 13)
echo "True \n";
if ($x == 12 or $y == 13)
echo "True \n";
if ($x == 10 xor $y == 9)
echo "True \n";
if ($x == 10 && $y == 13)
echo "True \n";
if ($x == 10 || $y == 14)
echo "True \n";
if (!($x == 19))
echo "True";
?>
Conditional or Ternary Operators in PHP:
The conditional operators are used to compare two values and take either of the results simultaneously, depending on whether the outcome is TRUE or FALSE.
Operator | Name | Example |
---|---|---|
? : | Ternary | If Condition is true ? Then value X : Otherwise value Y |
Example:
<?php
$x = 10;
echo ($x > 0) ? 'The number is positive' : 'The number is negative';
?>
Incrementing and Decrementing Operators in PHP:
The increment or decrement operators increase (increment) or decrease (decrement) the value of a variable. They are also called as unary operators because they work on just one operand. The list of these operators are as follows:
Operator | Name | Effect |
---|---|---|
++$a | Pre-increment | Increments $a by one, then returns $a |
$a++ | Post-increment | Returns $a, then increments $a by one |
--$a | Pre-decrement | Decrements $a by one, then returns $a |
$a-- | Post-decrement | Returns $a, then decrements $a by one |
Example:
<?php
$a = 20;
echo ++$a, "\n";  // Outputs: 21
echo $a, "\n";  // Outputs: 21
$a = 20;
echo $a++, "\n";  // Outputs: 20
echo $a, "\n";  // Outputs: 21
$a = 20;
echo --$a, "\n";  // Outputs: 19
echo $a, "\n";  // Outputs: 19
$a = 20;
echo $a--, "\n";  // Outputs: 20
echo $a, "\n";  // Outputs: 19
?>
Array Operators in PHP:
These operators are used to compare arrays. The list of array operators are as follows:
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $a + $b | Union of $a and $b |
== | Equality | $a == $b | True if $a and $b have the same key/value pairs |
=== | Identity | $a === $b | True if $a and $b have the same key/value pairs in the same order and of the same types |
!= | Inequality | $a != $b | True if $a is not equal to $b |
<> | Inequality | $a <> $b | True if $a is not equal to $b |
!== | Non-identity | $a !== $b | True if $a is not identical to $b |
Example:
<?php
$a = array("x" => "Apple", "y" => "Banana", "x" => "Mango");
$b = array("u" => "black", "v" => "white", "w" => "Yellow");
var_dump($a + $b);
var_dump($a == $b)+ "\n";
var_dump($a === $b)+ "\n";
var_dump($a != $b)+ "\n";
var_dump($a <> $b)+ "\n";
var_dump($a !== $b)+ "\n";
?>
String Operators in PHP:
The list of string operators are as follows:
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation | $a . $b | Concatenation of $a and $b |
.= | Concatenation assignment | $a .= $b | Appends the $b to the $a |
Example:
<?php
$a = "W3coding";
$b = "Schools";
echo $a . $b, "\n";  // Outputs: W3codingSchools
$a .= $b;
echo $a;  // Outputs: W3codingSchools
?>
Spaceship Operators in PHP
PHP 7 introduced a new kind of operator called a spaceship operator. Similarly to some other kinds of operators, it compares two values (integers, floats, or strings). However, it returns an integer instead of a boolean value.
Operator | Syntax | Operation |
---|---|---|
$a < $b | $a <=> $b | Identical to -1 (right is greater) |
$a > $b | $a <=> $b | Identical to 1 (left is greater) |
$a <= $b | $a <=> $b | Identical to -1 (right is greater) or identical to 0 (if both are equal) |
$a >= $b | $a <=> $b | Identical to 1 (if left is greater) or identical to 0 (if both are equal) |
$a == $b | $a <=> $b | Identical to 0 (both are equal) |
$a != $b | $a <=> $b | Not Identical to 0 |
Example:
<?php
echo(4 <=> 4);  //Output: 0
echo "\n";
echo(2 <=> 5);  //Output: -1
echo "\n";
echo(7 <=> 2);  //Output: 1
echo "\n";
?>