# PHP | Operators

Operators are used to perform operations on some values. In other words, we can describe operators as something that takes some values, performs some operation on them and gives a result. From example, “1 + 2 = 3” in this expression ‘+’ is an operator. It takes two values 1 and 2, performs addition operation on them to give 3.

Just like any other programming language, PHP also supports various types of operations like the arithmetic operations(addition, subtraction, etc), logical operations(AND, OR etc), Increment/Decrement Operations etc. Thus, PHP provides us with many operators to perform such operations on various operands or variables or values. These operators are nothing but symbols needed to perform operations of various types. Given below are the various groups of operators:

- Arithmetic Operators
- Logical or Relational Operators
- Comparison Operators
- Conditional or Ternary Operators
- Assignment Operators
- Spaceship Operators (Introduced in PHP 7)
- Array Operators
- Increment/Decrement Operators
- String Operators

Let us now learn about each of these operators in details:

**Arithmetic Operators**

The arithmetic operators are use to perform simple mathematical operations like addition, subtraction, multiplication etc. Below is the list of arithmetic operators along with there syntax and operations, that PHP provides us:

Operator | Name | Syntax | Operation |
---|---|---|---|

+ | Addition | $x + $y | Sum the operands |

– | Subtraction | $x – $y | Differences the operands |

* | Multiplication | $x * $y | Product of the operands |

/ | Division | $x / $y | Quotient of the operands |

** | Exponentiation | $x ** $y | $x raised to the power $y |

% | Modulus | $x % $y | Remainder of the operands |

Note: The exponentiation has been introduced in PHP 5.6.

Example:

`<?php` ` ` `// variable 1` `$x` `= 29;` ` ` `// variable 2` `$y` `= 4;` ` ` `// 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:

33 25 116 7.25 1

**Logical or Relational Operators**

These are basically used to operate with conditional statements and expressions. Conditional statements are based on conditions. Also, a condition can either be met or cannot be met so the result of a conditional statement can either be true or false. Here are the logical operators along with there syntax and operations, that PHP provides us:

Operator | Name | Syntax | Operation |
---|---|---|---|

and | Logical AND | $x and $y | True if both the operands are true else false |

or | Logical OR | $x or $y | True if either of the operand is true else false |

xor | Logical XOR | $x xor $y | True if either of the operand is true and false if both are true |

&& | Logical AND | $x && $y | True if both the operands are true else false |

|| | Logical OR | $x || $y | True if either of the operand is true else false |

! | Logical NOT | !$x | True if $x is false |

Example:

`<?php` ` ` `$x` `= 50;` `$y` `= 30;` ` ` `if` `(` `$x` `== 50 ` `and` `$y` `== 30)` ` ` `echo` `"and Success \n"` `;` ` ` `if` `(` `$x` `== 50 ` `or` `$y` `== 20)` ` ` `echo` `"or Success \n"` `;` ` ` `if` `(` `$x` `== 50 ` `xor` `$y` `== 20)` ` ` `echo` `"xor Success \n"` `;` ` ` `if` `(` `$x` `== 50 && ` `$y` `== 30)` ` ` `echo` `"&& Success \n"` `;` ` ` `if` `(` `$x` `== 50 || ` `$y` `== 20)` ` ` `echo` `"|| Success \n"` `;` ` ` `if` `(!` `$z` `)` ` ` `echo` `"! Success \n"` `;` ` ` `?> ` |

Output:

and Success or Success xor Success && Success || Success ! Success

**Comparison Operators**

These operators are used to compare two elements and outputs the result in boolean form. Here are the comparison operators along with there syntax and operations, that PHP provides us:

Operator | Name | Syntax | Operation |
---|---|---|---|

== | Equal To | $x == $y | Returns True if both the operands are equal |

!= | Not Equal To | $x != $y | Returns True if both the operands are not equal |

<> | Not Equal To | $x <> $y | Returns True if both the operands are unequal |

=== | Identical | $x === $y | Returns True if both the operands are equal and are of the same type |

!== | Not Identical | $x == $y | Returns True if both the operands are unequal and are of different types |

< | Less Than | $x < $y | Returns True if $x is less than $y |

> | Greater Than | $x > $y | Returns True if $x is greater than $y |

<= | Less Than or Equal To | $x <= $y | Returns True if $x is less than or equal to $y |

>= | Greater Than or Equal To | $x >= $y | Returns True if $x is greater than or equal to $y |

Example:

`<?php` ` ` `$a` `= 80;` `$b` `= 50;` `$c` `= ` `"80"` `;` ` ` `// Here var_dump function has been used to ` `// display structured information. We will learn ` `// about this function in complete details in further` `// 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)

**Conditional or Ternary Operators**

These operators are used to compare two values and take either of the result simultaneously, depending on whether the outcome is TRUE or FALSE. These are also used as shorthand notation for if…else statement that we will read in the article on decision making.**Syntax**:

$var = (condition)? value1 : value2;

Here, condition will either evaluate to true or false. If the condition evaluates to True, then value1 will be assigned to the variable $var otherwise value2 will be assigned to it.

Operator | Name | Operation |
---|---|---|

?: | Ternary | If condition is true ? then $x : or else $y. This means that if condition is true then left result of the colon is accepted otherwise the result on right. |

Example:

`<?php` ` ` `$x` `= -12;` ` ` `echo` `(` `$x` `> 0) ? ` `'The number is positive'` `: ` `'The number is negative'` `;` ` ` `?>` |

Output:

The number is negative

**Assignment Operators**

These operators are used to assign values to different variable, with or without mid-operations. Here are the assignment operators along with there syntax and operations, that PHP provides us:

Operator | Name | Syntax | Operation |
---|---|---|---|

= | Assign | $x = $y | Operand on the left obtains the value of the operand on right |

+= | Add then Assign | $x += $y | Simple Addition same as $x = $x + $y |

-= | Subtract then Assign | $x -= $y | Simple subtraction same as $x = $x – $y |

*= | Multiply then Assign | $x *= $y | Simple product same as $x = $x * $y |

/= | Divide then Assign (quotient) | $x /= $y | Simple division same as $x = $x / $y |

%= | Divide then Assign (remainder) | $x %= $y | Simple division same as $x = $x % $y |

Example:

`<?php` ` ` `// simple assign operator` `$y` `= 75;` `echo` `$y` `, ` `"\n"` `;` ` ` `// add then assign operator` `$y` `= 100;` `$y` `+= 200;` `echo` `$y` `, ` `"\n"` `;` ` ` `// subtract then assign operator` `$y` `= 70;` `$y` `-= 10;` `echo` `$y` `, ` `"\n"` `;` ` ` `// multiply then assign operator` `$y` `= 30;` `$y` `*= 20;` `echo` `$y` `, ` `"\n"` `;` ` ` `// Divide then assign(quotient) operator` `$y` `= 100;` `$y` `/= 5;` `echo` `$y` `, ` `"\n"` `;` ` ` `// Divide then assign(remainder) operator` `$y` `= 50;` `$y` `%= 5;` `echo` `$y` `;` ` ` `?>` |

Output:

75 300 60 600 20 0

**Array Operators**

These operators are used in case of arrays. Here are the array operators along with there syntax and operations, that PHP provides us:

Operator | Name | Syntax | Operation |
---|---|---|---|

+ | Union | $x + $y | Union of both i.e., $x and $y |

== | Equality | $x == $y | Returns true if both has same key-value pair |

!= | Inequality | $x != $y | Returs True if both are unequal |

=== | Identity | $x === $y | Returns True if both has same key-value pair in the same order and of same type |

!== | Non-Identity | $x !== $y | Returns True if both are not identical to each other |

<> | Inequality | $x <> $y | Returns True if both are unequal |

Example:

`<?php` ` ` `$x` `= ` `array` `(` `"k"` `=> ` `"Car"` `, ` `"l"` `=> ` `"Bike"` `);` `$y` `= ` `array` `(` `"a"` `=> ` `"Train"` `, ` `"b"` `=> ` `"Plane"` `);` ` ` `var_dump(` `$x` `+ ` `$y` `);` `var_dump(` `$x` `== ` `$y` `) + ` `"\n"` `;` `var_dump(` `$x` `!= ` `$y` `) + ` `"\n"` `;` `var_dump(` `$x` `<> ` `$y` `) + ` `"\n"` `;` `var_dump(` `$x` `=== ` `$y` `) + ` `"\n"` `;` `var_dump(` `$x` `!== ` `$y` `) + ` `"\n"` `;` ` ` `?>` |

Output:

array(4) { ["k"]=> string(3) "Car" ["l"]=> string(4) "Bike" ["a"]=> string(5) "Train" ["b"]=> string(5) "Plane" } bool(false) bool(true) bool(true) bool(false) bool(true)

**Increment/Decrement Operators**

These are called the unary operators as it work on single operands. These are used to increment or decrement values.

Operator | Name | Syntax | Operation |
---|---|---|---|

++ | Pre-Increment | ++$x | First increments $x by one, then return $x |

— | Pre-Decrement | –$x | First decrements $x by one, then return $x |

++ | Post-Increment | $x++ | First returns $x, then increment it by one |

— | Post-Decrement | $x– | First returns $x, then decrement it by one |

Example:

`<?php` ` ` `$x` `= 2;` `echo` `++` `$x` `, ` `" First increments then prints \n"` `;` `echo` `$x` `, ` `"\n"` `;` ` ` `$x` `= 2;` `echo` `$x` `++, ` `" First prints then increments \n"` `;` `echo` `$x` `, ` `"\n"` `;` ` ` `$x` `= 2;` `echo` `--` `$x` `, ` `" First decrements then prints \n"` `;` `echo` `$x` `, ` `"\n"` `;` ` ` `$x` `= 2;` `echo` `$x` `--, ` `" First prints then decrements \n"` `;` `echo` `$x` `;` ` ` `?>` |

Output:

3 First increments then prints 3 2 First prints then increments 3 1 First decrements then prints 1 2 First prints then decrements 1

**String Operators**

These are implemented over strings.

Operator | Name | Syntax | Operation |
---|---|---|---|

. | Concatenation | $x.$y | Concatenated $x and $y |

.= | Concatenation and assignment | $x.=$y | First concatenates then assigns, same as $x = $x.$y |

Example:

`<?php` ` ` `$x` `= ` `"Geeks"` `;` `$y` `= ` `"for"` `;` `$z` `= ` `"Geeks!!!"` `;` `echo` `$x` `. ` `$y` `. ` `$z` `, ` `"\n"` `;` ` ` `$x` `.= ` `$y` `. ` `$z` `;` `echo` `$x` `;` ` ` `?>` |

Output:

GeeksforGeeks!!! GeeksforGeeks!!!

**Spaceship Operators (Introduced in PHP 7)**

PHP 7 has introduced a new kind of operator called spaceship operator (). These operators are used to compare values but instead of returning boolean result, it returns integer values. If both the operands are equal, it returns 0. If the right operand is greater, it returns -1. If the left operand is greater, it returns 1. The following table shows how it works in detail:

Operator | Syntax | Operation |
---|---|---|

$x < $y | $x <=> $y | Identical to -1 (right is greater) |

$x > $y | $x <=> $y | Identical to 1 (left is greater) |

$x <= $y | $x <=> $y | Identical to -1 (right is greater) or identical to 0 (if both are equal) |

$x >= $y | $x <=> $y | Identical to 1 (if left is greater) or identical to 0 (if both are equal) |

$x == $y | $x <=> $y | Identical to 0 (both are equal) |

$x != $y | $x <=> $y | Not Identical to 0 |

Example:

`<?php` ` ` `$x` `= 50;` `$y` `= 50;` `$z` `= 25;` ` ` `echo` `$x` `<=> ` `$y` `;` `echo` `"\n"` `;` ` ` `echo` `$x` `<=> ` `$z` `;` `echo` `"\n"` `;` ` ` `echo` `$z` `<=> ` `$y` `;` `echo` `"\n"` `;` ` ` `// We can do the same for Strings` `$x` `= ` `"Ram"` `;` `$y` `= ` `"Krishna"` `;` ` ` `echo` `$x` `<=> ` `$y` `;` `echo` `"\n"` `;` ` ` `echo` `$x` `<=> ` `$y` `;` `echo` `"\n"` `;` ` ` `echo` `$y` `<=> ` `$x` `;` ` ` `?>` |

Output:

0 1 -1 1 1 -1

This article is contributed by **Chinmoy Lenka**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.