# PHP Operators

An operator is a symbol that takes one or more values (operands) and yields another value.
For example: 4 + 5 = 9. Here symbol “+” is an operator which takes 4 and 5 as values(operands) for operation addition and result 9 as output.

### PHP language supports operators in the following groups:

1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Increment/Decrement operators
5. Logical operators
6. String operators
7. Array operators
8. Conditional assignment operators

## 1. PHP Arithmetic Operators

The arithmetic operators are used to perform common arithmetic operations such as addition, subtraction, multiplication etc.

```<?php

\$x = 14;
\$y = 4;

echo(\$x + \$y); // 0utputs: 18

//Substraction
echo(\$x - \$y); // 0utputs: 10

//Multiplication
echo(\$x * \$y); // 0utputs: 56

//Division
echo(\$x / \$y); // 0utputs: 3

//Modulus
echo(\$x % \$y); // 0utputs: 2

?>```

## 2. PHP Assignment Operators

The Assignment operators are used to assign values to different variable.

 Operators Description Example = Assigns values from right side to left side operand x=y += Add & Assignment operator. It adds right operand to the left operand & assigns result to the left operand. x +=y -= Substract & Assignment operator. It substract right operand to the left operand & assigns result to the left operand. x -=y *= Multiply & assignment operator. It multiplies the right operand to the left operand & assigns result to the left operand. x *=y /= Divide & assignment operator. It divides left operand with the right operand & assigns the result to the left operand. x /=y %= Modulus & assignment operator. It makes modulus using two operands & assigns the value to the left operands. x %=y

## 3. PHP Comparison Operators

The PHP Comparison Operators are used to compare two values.

## 4. PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value. And PHP decrement operators are used to decrements a variable’s value.

## 5. PHP Logical Operators

Logical operators are very important as it comes use in real world program. The PHP logical operators are used to combine conditional statements.

## 6. PHP String Operators

There are two string operators. The first is String Operator(‘.’) and the second one is concatenating assignment operator (‘.=’).

• . Concatenation
• .= Combined Concatenation

Let’s us understand with example –

Concatenation Example (‘.’) –

```<?php
\$firstname = "Share";
\$lastname = "Query";
echo \$firstname . " ". \$lastname ;
?>

Output: Share Query```

Combined Concatenation Example (‘.=’) –

```<?php
\$mystring = 'Welcome to ';
\$mystring .= 'Share ';
\$mystring .= 'Query';
echo \$mystring;
?>

Output: Welcome to Share Query```

## 7. PHP Array Operators

The PHP array operators are used to compare with arrays.

 Operator Name Example Result + Union \$x + \$y Union of \$x and \$y == Equality \$x == \$y Returns true if \$x & \$y have the same key /value pairs. === Identical \$x ===\$y Returns true if \$x & \$y have the same key/value pairs in the same order & of same types. != Inequality \$x != \$y Returns true if array \$x is not equal to array \$y. <> Inequality \$x <> \$y Returns true if array \$x is not equal to \$y. !== Non-identical \$x !== \$y Returns true if array \$x is not identical to array \$y.

## 8. PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions/expressions.

There are two conditional assignment operators in PHP –

1. Ternary (‘?:’) –
2. Null Coalescing (‘??’) –

Ternary Operator – Ternary operator actually means “an operator which involves on three operands.

The Ternary Operator will result lefthand operand when the condition evaluates to true. And the righthand operand is used for default or false values.

``\$result = \$condition ? \$condition : 'default';``

Example –

```<?php

//if empty(\$user) = TRUE
echo \$status;

//if empty(\$user) = FALSE:
\$user = "Kumud";
echo \$status;
//Output: Logged In

?>```

Null Coalescing – Null Coalescing operator (??) is used to replace the ternary operator with the help of isset() function. The Null coalescing operator returns its first operand if it exists & it is not NULL otherwise it will return the second operand.

Example –

```<?php

//If user is not logged in