The PHP operators are inspired by the operators used in C. Here is a non-exhaustive list:
| Operator | Description | Comment |
|---|---|---|
| + | addition | |
| - | subtraction | |
| * | multiplication | |
| / | division | |
| % | modulo | Rest of the division between two integers |
| ** | exponential |
| Operator | Description | Comment |
|---|---|---|
| . | concatenate two strings |
| 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; |
| 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 |
| Operator | Description | Comment |
|---|---|---|
| & | bitwise AND | |
| | | bitwise OR | |
| ^ | bitwise XOR | |
| ~ | bitwise NOT | |
| << | bitwise left Shift | |
| >> | bitwise right Shift |
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 |
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";
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>
$begin, $middle and $end.$string, $integer and $float. Display the result.Here is the expected result:
