The assignment operators are used to assign values to variables or set a variable to another variable's value. The basic assignment operator is "=".
For example:
$my_var = "hello";
$another_var = $my_var;
Operator | Description | Example | As if we had said |
---|---|---|---|
= | Assign | $x = $y | $x = $y |
+= | Add and assign | $x += $y | $x = $x + $y |
-= | Subtract and assign | $x -= $y | $x = $x - $y |
*= | Multiply and assign | $x *= $y | $x = $x * $y |
/= | Divide and assign quotient | $x /= $y | $x = $x / $y |
%= | Divide and assign modulus | $x %= $y | $x = $x % $y |
&= | Bitwise And and assign | $x &= $y | $x = $x & $y |
|= | Bitwise Or and assign | $x | = $y |
^= | Bitwise XOr and assign | $x ^= $y | $x = $x ^ $y |
.= | concatenate String and assign | $x .= $y | $x = $x . $y |
<?php $a = 3; $a += 5; // as if we had said: $a = $a + 5; $b = "Hello "; $b .= "Lautturi.com!"; // just like $b = $b . "Lautturi.com!"; $c = 3; // 0011 $c |= $a; // as if we had said: $c = $c | $a; var_dump($a);// int(8) var_dump($b);// string(19) "Hello Lautturi.com!" var_dump($c);// int(11) ?>