# Operators in PHP

What is an operator? The simple answer from the 4 + 5 expression is equal to 9 . Here, 4 and 5 are called operands and + is called operator. The PHP language supports the following types of operators:

• Arithmetic operators
• Comparison operator
• Logical operator (or relational operator)
• The assignment operator
• The conditional operator (or the 3-person operator) Operators in PHP

## Arithmetic operators in PHP

The following table lists the arithmetic operators supported by PHP language:

Assume variable A holds 10, variable B holds 20 then:

For example

Operator Describe For example
+ Adds two operands A + B will give 30
- Subtracts the second operand from the first operand A - B will give -10
* Multiply two operands A * B will give 200
/ Division B / A will give 2
% Balance calculation B% A will give 0
++ The increment operator increases the operand value by one unit A ++ will give 11
- Decrement operator reduces the value of the operand by one unit A-- the result is 9

## Comparison operator in PHP

The following table lists the comparison operators supported by PHP language. Assume variable A holds 10, variable B holds 20, then:

For example

Operator Describe For example
== Checks if 2 operands are equal or not. If equal then condition becomes true. (A == B) is not true.
! = Checks whether 2 operands have different values. If not then condition becomes true. (A! = B) is true.
> Checks if the value of left operand is greater than the value of right operand. If greater then condition becomes true. (A> B) is not true.
< Checks if the left operand is less than the right operand. If less then true. (A <B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand. If true is true. (A> = B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand. If true is true. (A <= B) is true.

## Logical operators in PHP

The following table specifies all the logical operators supported by the PHP language. Assume variable A holds 10 and variable B holds 20, then:

For example

Operator Describe For example
and Called the Logic AND operator. If both operands are true then condition becomes true (A and B) là true.
or Called the Logic OR operator. If either of the operands is true then condition becomes true (A or B) là true.
&& Called the Logic AND operator. If both operands are true then condition becomes true (A && B) là true.
|| Called the Logic OR operator. If either of the operands is true then condition becomes true (A || B) is true.
! Called the Logic NOT operator. Used to reverse the logical state of the operand. If the condition is true then the Logic NOT operator will return false !(A && B) là false.

## The assignment operator in PHP

Following are the assignment operators supported by PHP language:

For example

Operator Describe For example
= Simple assignment operator. Assigns the value of the right operand to the left operand C = A + B will assign the value of A + B into C
+ = Adds the right operand to the left operand and assigns the value to the left operand C + = A is equivalent to C = C + A
- = Subtracts the right operand from the left operand and assigns the value to the left operand C - = A is equivalent to C = C - A
* = Multiply the right operand by the left operand and assign this value to the left operand C * = A is equivalent to C = C * A
/ = Divide the left operand by the right operand and assign this value to the left operand C / = A is equivalent to C = C / A
% = Take the remainder of the left operand with the right operand and assign it to the left operand C% = A is equivalent to C = C% A

## The conditional operator in PHP

There is more than one operator known as the conditional operator. First, it evaluates an expression to true or false and then executes one of the given commands depending on the outcome of the evaluation. The conditional operator has the following syntax:

For example

Operator Describe For example
? : Conditional expression If the condition is true? Then value X: Otherwise value Y

## Classifying operators in PHP

All of the above operators can be classified into the following types in PHP:

Unary operator, which precedes an operand.

The binary operator, which takes two operands and performs various arithmetic and logical operations.

The conditional operator (ternary operator), or triangular operator, takes three operands and evaluates either the second expression or the third expression, depending on the estimated result of the first expression.

Assignment operator, which assigns a value to a variable.

## Operator precedence in PHP

Operator precedence in PHP determines how expressions are calculated. For example, the multiplication operator has precedence over the addition operator, and it is done first.

For example, x = 7 + 3 * 2; Here, x is assigned the value 13, not 20 because operator * has a higher precedence than +, so it first multiplies 3 * 2 and then adds 7.

The following table lists the precedence of operators in PHP. The operators with the highest precedence appear at the top of the table, and the operators with the lowest precedence are at the bottom of the table. In an expression, the operators with the highest precedence are calculated first.

Species Operator Priority
Unary ! ++ - Right to left
Multiplication * /% Left to right
Add up + - Left to right
relationship < <= > >= Left to right
Calculated by ==! = Left to right
Logic AND && Left to right
Logic OR || Left to right
Condition ?: Right to left
Assign = + = - = * = / =% = Right to left