# PHP Operators

Updated Jan 9, 2021 View by 840

In this tutorial, you will learn about PHP Operators** **and how they are used in programs alongside their functionalities. An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).

**PHP Operators**

**PHP O****perators** are symbols that tell the compiler to perform certain mathematical or logical manipulation. In simple terms, we can say operators are used to manipulating Data and Variables.

For example: `+,`

`-`

are the operators used for mathematical calculation.

## List of PHP Operators

- Arithmetic Operator
- Relational Operator
- Logical Operator
- Assignment Operator
- Increment/Decrement Operator
- Comparison Operator
- Bitwise Operator
- String Operators
- Array Operators

The data items in which any operation is carried out are called operands.

x + y

Here x and y are operands and + is an operator and calculating sum is an operation.

Operators which require two operands are called binary operators and which takes single operand are called unary operators.

## 1) Arithmetic operators in PHP

Operators used in the arithmetic operation of addition, subtraction, multiplication or division are called arithmetic operators.

Arithmetic operators | Meaning |
---|---|

+ | Addition or unary plus |

– | Subtraction or unary minus |

* | Multiplication |

/ | Division |

% | Modulus division |

## 2) Relational Operator in PHP

Relational operators are used to comparing two operators depending on their relation.

For example, for comparing the price of two things. The value of a relational operator is either or`1`

`0`

. If the specified relation is true then else`1`

`0`

.

a > b

Here, `>`

is a relational operator and it will return if `1`

`a`

is greater than else`b`

it will return.`0`

Relational operator | Meaning |
---|---|

< | is less than |

<= | is less than or equal to |

> | is greater than |

>= | is greater than or equal to |

== | is equal to |

!= | is not equal to |

## 3) Logical Operator in PHP

Logical operators are used when more than one condition is tested. They are used to combine more than one relational expressions.

a > b && c == 1

Here && is relational operator used to combine two relational expressions a > b and c == 1.

Logical Operator | Example | Meaning |
---|---|---|

AND | $a and $b | TRUE if both $a and $b are TRUE. |

OR | $a or $b | TRUE if either $a or $b is TRUE. |

XOR | $a xor $b | TRUE if either $a or $b is TRUE, but not both. |

NOT | ! $a | TRUE if $a is not TRUE. |

AND | $a && $b | TRUE if both $a and $b are TRUE. |

OR | $a || $b | TRUE if either $a or $b is TRUE. |

The reason for the two different variations of “and” and “or” operators is that they operate at different precedences.

## 4) Assignment Operators in PHP

The basic assignment operator is “=”. Your first inclination might be to think of this as “equal to”. Don’t. It really means that the left operand gets set to the value of the expression on the right (that is, “gets set to”).

The value of an assignment expression is the value assigned. That is, the value of “*$a = 3*” is 3. This allows you to do some tricky things:

<?php $a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4. ?>

Assignment Operators | Example | Meaning |
---|---|---|

= | x = y | The left operand gets set to the value of the expression on the right |

+= | x = x + y | Addition |

-= | x = x – y | Subtraction |

*= | x = x * y | Multiplication |

/= | x = x / y | Division |

%= | x = x % y | Modulus |

**Note:** Don’t get confused between equality operator == and assignment operator =.

## 5) Bitwise Operator in PHP

Bitwise operators are used to manipulating data at a bit level. They are used for testing or shifting the bit.

Bitwise Operator | Meaning |
---|---|

< | bitwise AND |

| | bitwise OR |

^ | bitwise exclusive OR |

<< | shift left |

>> | shift right |

## 6) Increment / Decrement Operator in PHP

PHP provides an increment PHP Operator and `++`

decrement PHP Operator. `--`

The functionality of is to` ++`

add 1 unit to the operand and is `--`

to subtract 1 from the operand.\

++ $a; -- $b;

Here ++a is equivalent to a = a + 1 and –b is equivalent to b = b – 1.

- There are two kinds of increment and decrement operator i.e
**prefix and postfix.** - If the operator is used before the variable i.e ++a then it is called
**prefix increment operator.** - If the operator is used after variable i.e a++ then it is called
**postfix increment operator.** - In the prefix operator, first 1 is added and then the value is assigned to the variable.
- In postfix operator, first the value is assigned then only 1 is added and the added value is assigned.

Increment / Decrement Operator | Example | Meaning |
---|---|---|

Pre-increment | ++$a | Increments $a by one, then returns $a. |

Post-increment | $a++ | Returns $a, then increments $a by one. |

Pre-decrement | –$a | Decrements $a by one, then returns $a. |

Post-decrement | $a– | Returns $a, then decrements $a by one. |

**Note:** The increment/decrement operators only affect numbers and strings. Arrays, objects, and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.

## 7) Comparison Operator in PHP

Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.

Comparison Operator | Example | Meaning |
---|---|---|

Equal | $a == $b | TRUE if $a is equal to $b after type juggling. |

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 after type juggling. |

Not equal | $a <> $b | TRUE if $a is not equal to $b after type juggling. |

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 strictly less than $b. |

Greater than | $a > $b | TRUE if $a is strictly greater than $b. |

Less than or equal to | $a <= $b | TRUE if $a is less than or equal to $b. |

Greater than or equal to | $a >= $b | TRUE if $a is greater than or equal to $b. |

Spaceship | $a <=> $b | An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7. |

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

<?php var_dump(0 == "a"); // 0 == 0 -> true var_dump("1" == "01"); // 1 == 1 -> true var_dump("10" == "1e1"); // 10 == 10 -> true var_dump(100 == "1e2"); // 100 == 100 -> true switch ("a") { case 0: echo "0"; break; case "a": // never reached because "a" is already matched with 0 echo "a"; break; } ?>

## 8) String Operators in PHP

There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘*.=*‘), which appends the argument on the right side to the argument on the left side.

<?php $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello "; $a .= "World!"; // now $a contains "Hello World!" ?>

PHP has two operators that are specially designed for strings.

String Operators | Example | Meaning |
---|---|---|

Concatenation(.) | $txt1 . $txt2 | Concatenation of $txt1 and $txt2 |

Concatenation assignment(.=) | $txt1 .= $txt2 | Appends $txt2 to $txt1 |

## 9) Array Operators in PHP

The PHP array operators are used to compare arrays.

<?php $a = array("apple", "banana"); $b = array(1 => "banana", "0" => "apple"); var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(false) ?>