# PHP - Operators ## PHP - Operators

Operators are used for performing various functions or operations on variables and values. PHP supports a number of operator types to get the desired output. Some of the operators are defined below-

• Arithmetic operators
• Logical operator
• Conditional operator
• Assignment operator
• Comparison operator

### 1. Arithmetic operators

Arithmetic operators perform arithmetic operations on variables and on the numeric data. The concatenate operator also works in case of works on strings values. PHP supports the following arithmetic operators for provided two variable x and y. These operators are not only confined to two variable but as an example, we are using two variables.

 Operator Name Description Example Output + Addition Provide a summation of x and y 2 + 1; 3 - Subtraction Provide a difference of x and y 2 – 1; 1 * Multiplication Provide a Multiplication of x and y 3 * 1; 3 / Division Provide the quotient of x and y 45 / 3; 15 % Php Modulus Provides the reminder of diving x and y 10 % 6; 4 -n Negation Turns n into a negative number provided n is positive -(-2); 2 x. y Concatenation Connect together x and y "PHP" . " Operator";10 . 3; PHP Operator103

### 2. Assignment Operators

Assignment operators are basically used for assigning values to variables that can also be used together with arithmetic operators.

 Operator Name Description Example Output x = ? assignment Assigns the value of ? to x \$x = 2; 2 x += ? addition Increments the value of x by ? \$x = 1;\$x += 2; 3 X -= ? subtraction Subtracts ? from the value of x \$x = 9;\$x -= 2; 7 X *=? multiplication Multiplies the value of x by ? times \$x = 1;\$x *=9; 9 X /=? division Provides the quotient of x and ? \$x = 9;\$x /=3; 3 X %=? modulus Provides reminder of dividing x by? \$x = 5;\$x %= 2; 1 X .=? concatenate Puts together items " \$x = ‘Name’;\$x .= ‘ Full!’;" NameFull

### 3. Comparison operators

Comparison operators are used to comparing values, variables, and data types. Below are the comparison operators that are supported by PHP:

 Operator Name Description Example Output X == y Equal Compares the value of x and y then returns true if they are equal 3 == "3"; True or 1 X === y identical Compares both values and data types. 1 === "1"; False or 0. Since 1 is integer and “1” is string X != y, x <> y PHP Not equal Compares the values of x and y. returns true if the values are not equal 3 != 1; True or 1 X > y Greater than Compares the values of x and y. returns true if x is greater than y 2 > 1; True or 1 X < y Less than Compares the values of x and y. returns true if x is less than y 5 < 1; False or 0 X >= y Greater than or equal Compares values of x and y. returns true if x is greater than or equal to y 2 >=2 True or 1 X <= y Less than or equal Compares values of x and y. returns true if x is greater than or equal to y 9 <= 6 False or 0

### 4. Logical operators

If you are working with logical operators, any number greater than or less than zero (0) will evaluate to true. Zero (0) evaluates to false.

 Operator Name Description Example Output X and y, x && y And Returns the true value if both x and y are equal 1 and 5;True&& False; True or 1False or 0 X or y, x || y Or Returns the true value if either x or y is true 6 or 9;0 || 0; True or 1False or 0 X xor y Exclusive or, xor Returns the true value if the only x is true or the only y is true 2 xor 2;1 xor 0; False or 0True or 1 !x Not Returns the true value if x is false and false if x is true !0; True or 1

### 5. Conditional Operator

A conditional operator generally used within conditional expressions that evaluates an expression for a true or false value which will lead to the execution of a given set of instructions that depend on the result of the evaluation. The conditional operator has below mentioned syntax ?

 Operator Description Example ? : Conditional Expression If Condition is true? Then value X: Otherwise value Y

## Precedence of PHP Operators

Operator precedence shows how the terms or the variables have to be grouped within an expression. This affects how an expression is evaluated or in what order the operators will get executed. Certain operators have higher precedence than others and the higher precedence operator will be executed first than the other operator. In case there are two operators with the same precedence then they will get executed left to right. The below table states the operators with higher precedence at the top and decreasing.

 Category Operator Associativity Unary ! ++ -- Right to left Multiplicative * / % Left to right Additive + - Left to right Relational < <= > >= Left to right Equality == != Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %= Right to left