There are 12 types of operators in PHP. We will review them using summary tables. Let’s start by arithmetic operators.

We’ll be covering the following topics in this tutorial:

## Arithmetic operators

Arithmetic operators are used to perform mathematical operations on variables. They are naturally all conventional operations such as addition, multiplication, subtraction or division. The following table summarizes all the possible mathematical operations.

Operator | Operation | Example | Result |

– | Negation | -$a | $Has opposite |

+ | Addition | $a + $b | Sum of $a and $b |

* | Multiplication | $a * $b | Product of $a and $b |

– | Subtraction | $a – $b | Difference of $a and $b |

/ | Division | $a / $b | Quotient of $a and $b |

% | Modulus | $a % $b | Remaining $a /$b |

**Note:** The modulus operator returns the remainder of the division between two numbers. Its use allows for example to determine the parity of a number or to achieve alternating colors of the lines of an HTML table.

## Increment operators / decrement

The increment operator (respectively decrement) increases (respectively decrease) by one unit the value of the variable. Are used essentially in the loops to update the counter value in each iteration.

Operator | Operation | Example | Result |

++ | Pre-Increment | ++$a | Increments $a, then returns $a |

++ | Post-Increment | $a++ | Returns $a,then increments $a |

— | Pre-Decrement | –$a | Decrements $a, then returns $a |

— | Post-Decrement | $a– | Returns $a, then decrements $a |

These operators can be placed either before or after the variable one. This nuance perhaps interesting when using loop.

Generally, the use cases are that one employs over $a++ ($a– respectively).

## The assignment operator

This is probably the most basic and essential operator of the PHP language. It is through him that one assigns a value to a variable, which creates an array or that we instantiate a class.

Operator | Operation | Example | Result |

= | Assignment | $a = 3 | Sets the value a to $3 |

## The character string operators

They are two in number. The first is the concatenation operator realized by the period (.) While the second is the concatenating assignment operator by. =

Operator | Operation | Example | Result |

. | Concatenation | $a . $b | Concatenates the values of$a and $b |

.= | ConcatenatingAssignment | $a .= $b | Adds the value of $b following that of $a |

## Comparison operators

They are mainly used in conditional structures (if, elseif, else, for, while, …) to compare values between them. Its tests will return TRUE (true) if the comparison is true or false (false) if the comparison is false.

Operator | Operation | Example | Result |

== | Equalvalue | $a == $b | Verifies that the values $a and $b are identical |

=== | Equalin value and Type | $a === $b | Check the values and types of $a and $b are identical |

!= | Differenceinvalue | $a != $b | Verifies that the values $a and $ b are different |

!== | Differenceinvalue and type | $a !== $b | Check the values and types of $a and $b are different |

<> | Differenceinvalue | $a <> $b | Alias! = |

< | strictinferiority | $a < $b | Verifies that $a is strictly less than $b |

<= | Less than or equal | $a <= $b | Verifies that $a is strictly less than or equal to $b |

> | strictsuperiority | $a > $b | Verifies that $a is strictly greater than $b |

>= | Greater than or equal | $a >= $b | Verifies that $a is strictly greater than or equal to $b |

**Note:** Do not confuse the assignment operator that works with a single equal sign (=) and which fixes the value of a variable.

When it is desired to compare both the values and types of variables, the operator is used === (respectively! ==). This check is recommended for two reasons:

• This ensures that the two variables are the same type.

• This is slightly faster execution than its sister who does not check the type.

To illustrate the distinction between the two forms of comparison:

Comparison test on variables<?php $a = '2'; // Type string $b = 2; // Integer type $c = 2; // Integer type

// Compare the values

if ($a == $b)

{

echo ‘$a and $b have the same value!’;

}

// Compare the values and types

if ($a === $c)

{

echo ‘$a and $c are of the same value and type’;

}

?>

These two examples introduce the concept of control structures provided by which we will study in the next tutorial. We have declared three variables the same value of 2. Only $ a is of type string (character string) while the other two are integer (int). When executing these two tests, we find that only the first displays. This is because double equals sign is not ensuring that the type of the two variables is the same contrast to the triple equal sign.

Logically, a string should not be “equal” to an integer. It would be like comparing apples and oranges !!! Here we see a weakness in terms of typing PHP variables, so this is why we speak of weak typing in the PHP language.

We advise you therefore to the extent possible to check your values on both types and values.

Tip: PHP introduced intval () function which allows for the cast (or cast). It transforms an integer such as natural whole character string.

## Logical operators

Logical operators are often used in control structures. They help to define more or less complex expressions that will return a boolean TRUE or FALSE. Below is a summary table of these operators.

Operator | Operation | Example | Result |

&& | AND | $a && $b | TRUE if $a and $b are true |

AND | AND | $a AND $b | && Aliases |

|| | OR | $a || $b | If $a or $b is true |

OR | OR | $a OR $b | || Aliases |

XOR | XOR | $a XOR $b | If $a or $b is true, but not both |

! | NO | !$a | If $a is false |

The difference between AND and && (respectively OR and ||) is the priority of execution. The && and || operators have a higher priority compared to their respective literal similar.

## Binary operators (bitwise)

These operators allow you to manipulate bits in an integer. If the left and right parameters are strings, the bitwise operator will act on the ASCII values of those characters.

Operator | Operation | Example | Result |

& | AND | $a & $b | The bits set to 1 in $a and $b are set to 1 |

| | OR | $a | $b | The bits set to 1 in$a or $b are set to 1 |

^ | XOR | $a ^ $b | The bits set to 1 in $a or $b but not both are set to 1 |

~ | NO | $a ~ $b | The bits that are set to 1 in $a is set to 0, and vice versa. |

<< | Left shift | $a << $b | Shifts the bits of $a $b steps to the left(each step means a multiplication by 2) |

>> | Right shift | $a >> $b | Shifting the bits of $a $b steps to the right(each step means division by 2) |

## The Combined Operators

The combined operators are operators combining arithmetic or binary operation with a summons. Hence the concept of combination. The following table summarizes these operators.

Operator | Operation | Example | Result |

+= | Addition | $a += 4 | Adds 4 to the value of $a and store the result in $a |

-= | Subtraction | $a -= 4 | Subtract 4 tothe value of $a and store the result in $a |

*= | Multiplication | $a *= 4 | Multiplied by the $4 of value and stores the result in $a |

/= | Division | $a /= 4 | Divided by the $4 of value and stores the result in $a |

%= | Modulus | $a %= 4 | Calculates the remainder of the division has $4 and stores the result in$a |

&= | ANDbinary | $a &= $b | Equal to $a =$a & $b |

|= | OR binary | $a |= $b | Equal to $a =$a |$b |

^= | XOR binary | $a ^= $b | Equal to $a = $a ^ $b |

<<= | Left shift | $a <<= $b | Equal to $a = $a << $b |

>>= | Right shift | $a >>= $b | Equal to $a = $a >> $b |

Generalizing all, it has obtained $ (operator) = $b which is also equivalent to $a = $a (operator) $b.

## The error control operator

It is the operator at sign (@) that removes all the errors generated by an expression (function, variables, constants, …). It is just up to the term for which you wish to hide the error that is thrown.

Operator | Operation | Example | Result |

@ | error | @include(‘file.php’) | Maskthe errorgenerated by theinclude () |

However this practice is strongly discouraged because we should not hide an error generated but rather it should be treated. However there are special cases where the use of this technique is useful. For example, when using fsockopen ().

## The execution operator

This operator, delimited by apostrophes or backtick, allows you to run shell commands. It behaves the same way as the shell_exec () function. If it is turned off or if the safe mode then the operator will not work for safety reasons (on shared accommodation for example).

Operator | Operation | Example | Result |

` ` | Shell | `ifconfig` | Executes theshell commandand returns the result |

The result of an executed shell commands can be restored as well to standard output in a variable. Illustration by an example from the official documentation of the executing operator.

**Using the runtime operator**

<?php $output = 'ls -at'; echo "<pre> $output </ pre>"; ?>

## Operators on arrays

As for the strings, PHP offers a series of operators to manipulate the tables. These operators are the same as the comparison operators do not necessarily have the same function and the same meaning.

Operator | Operation | Example | Result |

+ | union | $a + $b | Union of $a and $b |

== | Equality | $a == $b | TRUE if $a and $b contain the same key / value pairs |

=== | identical | $a === $b | TRUE if $a and $b contain the same key / value pairs in the same order and type |

!= | inequality | $a != $b | TRUE if $a and $b are not equal |

<> | inequality | $a <> $b | Alias! = |

!== | not identical | $a !== $b | TRUE if $a and $b are not identical |

## The object type operators (instance of)

The object type operator (= instanceof “Instance”) used in object-oriented programming to determine if an object, its parent and its derived classes are the same type or not.

Operator | Operation | Example | Result |

instanceof | Instance | $a instance of b | If $a is an instance of class b |

Take a simple example to illustrate this concept.

**Using the instance of operator**

`<?php`

// Class declaration

class Student {}

class Professor {}

// Instantiating an object of type Student

$Karan = new Student ();

// Testing the type of the object

if ($Karan instanceof Student) {

echo ‘Karan is a student!’;

}

else

{

echo ‘Karan is a teacher!’;

}

?>

In our example, you create an object $Karan type of Student (= instance of a Student class). Then we test whether that object is of type Student with the instanceof operator. If the test returns TRUE then $Karan is of type Student, otherwise it is of type Professor.

## Operator precedence

As in mathematics, operators prioritize them when executed. The following table summarizes each of the priorities of these operators.

Priority | Operator |

1 | new |

2 | () And [] |

3 | – ++ And! |

4 | ~ – (Int) (float) (string) (array) (object) @ |

5 | instanceof |

6 | * / And% |

7 | + – And. |

8 | << And >> |

9 | <<= And> => |

10 | ==! = === And |

11 | & |

12 | ^ |

13 | | |

14 | && |

15 | || |

16 | ? and: |

17 | Combined assignment operators |

18 | AND |

19 | XOR |

20 | OR |