# Lesson 3.5. PHP operators

## Operator list

The PHP operators are inspired by the operators used in C. Here is a non-exhaustive list:

## Arithmetic operators

Operator Description Comment
+ addition
- subtraction
* multiplication
/ division
% modulo Rest of the division between two integers
** exponential

## String operator

Operator Description Comment
. concatenate two strings

## Assignment operators

Operator Description Comment
= assignment `\$a = 5;` the integer 5 is assigned to the variable `\$a`
+= addition and assignment in short form `\$a += \$b` is equivalent to `\$a = \$a + \$b;`
-= subtraction and assignment in short form `\$a -= \$b` is equivalent to `\$a = \$a - \$b;`
*= multiplication and assignment in short form `\$a *= \$b` is equivalent to `\$a = \$a * \$b;`
/= division and assignment in short form `\$a /= \$b` is equivalent to `\$a = \$a / \$b;`
%= modulo and assignment in short form `\$a %= \$b` is equivalent to `\$a = \$a % \$b;`
.= concaténation and assignment in short form `\$a .= \$b` is equivalent to `\$a = \$a . \$b;`

## Comparison operators

Operator Description Comment
== equal to Same values
=== equal to Same types and values
!= not equal to Different values
<> not equal to Different type and value
!== not equal to Different type or value
< less than
> greater than
<= less or equal to
>= greater or equal to
<=> an integer less, equal or greater than zero
`\$a <=> \$b` => when `\$a` is respectively less, equal, or greater than `\$b`.
Since PHP 7

## Bitwise operators

Operator Description Comment
& bitwise AND
| bitwise OR
^ bitwise XOR
~ bitwise NOT
<< bitwise left Shift
>> bitwise right Shift

## Logical operators

Unlike bitwise operators, the operators below returns only two values: `TRUE` or `FALSE`.

Operator Description Comment
and ou && logical AND
or ou || logical OR
xor logical XOR
! logical NOT

## Example

Here is an example of some operators:

``````\$a = '6';
\$b = 4 + 2;

// True
if (\$a==\$b) echo "== Same values\n";
// False
if (\$a===\$b) echo "=== Same values and same type\n";

// False
if (\$a!=\$b) echo "!= Not equal\n";
// Faux
if (\$a<>\$b) echo "<> Different value and type\n";
// Vrai
if (\$a!==\$b) echo "!== Different value or type\n"; ``````

## Exercice

Let's consider the following PHP variable declarations:

``````<pre>
<?php
\$begin = "I love this ";
\$middle = "class, PHP is";
\$end = " awesome!";

\$string = '5';
\$integer = 6;
\$float = 3.2;
?>
</pre>``````
• Concatanate and display `\$begin`, `\$middle` and `\$end`.
• Add `\$string`, `\$integer` and `\$float`. Display the result.
• Calculate 2 power 10, display the result.

Here is the expected result:

## See also

Last update : 03/10/2022