How to use Assignment Operator in PHP
In this article, I will explain how the Assignment operator can be used in PHP.
PHP Operators
PHP language supports following type of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Assignment Operators
The Assignment operators are summarized in the following table.
Operator |
Same AS |
Description |
= |
x = y |
Simple assignment operator, Assigns values from right side operands to left side operand |
+= |
x =x + y |
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand |
-= |
x =x - y |
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand |
*= |
x =x * y |
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand |
/= |
x =x / y |
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand |
%= |
x =x % y |
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand |
Example
<html>
<head><title>Assignment Operators</title><head>
<body>
<?php
$x = 42;
$y = 20;
$c = $x + $y;
echo "Addtion Operxtion Result: $c <br/>";
$c += $x;
echo "Assignment Operation AND Add Result: $c <br/>";
$c -= $x;
echo "Assignment Operation AND Subtract Result: $c <br/>";
$c *= $x; echo "Assignment Operation AND Multiply Result: $c <br/>";
$c /= $x; echo "Assignment Operation AND Division : $c <br/>";
$c %= $x;
echo "Assignment Operation AND Modulus : $c <br/>";
?>
</body>
</html>
|
Output

You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here