Assignment
In PHP, you can use the assignment operator (=) to assign a value to a variable. For example:
$x = 10;
This will assign the value 10 to the variable $x.
You can also use compound assignment operators to perform an operation and assign the result to a variable in a single statement. For example:
$x = 10;
$x += 5; // equivalent to $x = $x + 5;
This will add 5 to the value of $x, which is 10, and assign the result (15) to $x.
Here are the compound assignment operators available in PHP:
- +=: Addition assignment
- -=: Subtraction assignment
- *=: Multiplication assignment
- /=: Division assignment
- %=: Modulus assignment
- .=: Concatenation assignment
Example:
<?php
$x = 10;
$y = 20;
$x += 5; // 15
$y -= 5; // 15
$x *= 2; // 30
$y /= 2; // 10
$x %= 3; // 0
$y .= " apples"; // "10 apples"
echo $x . "\n";
echo $y . "\n";
?>