Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to"). Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other.

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

<html>
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$a = ($b = 3) + 4; // $a is equal to 7 now, and $b has been set to 3.
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
$a = 3;
$a += 4; // sets $a to 7, as if we had said: $a = $a + 4;
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
echo "The value of variable a+=b is: ", $a+=$b, "<br /><br />";
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
echo "The value of variable a-=b is: ", $a-=$b, "<br /><br />";
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
echo "The value of variable a*=b is: ", $a*=$b, "<br /><br />";
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
echo "The value of variable a/=b is: ", $a/=$b, "<br /><br />";
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
echo "The value of variable a%=b is: ", $a%=$b, "<br /><br />";
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
echo "The value of variable a.=b is: ", $a.=$b, "<br /><br />";
$b = "Hello ";
$b .= "World"; // sets $b to "Hello World", just like $b = $b . "World";
echo "The value of variable b is: $b<br />";
$a = 'a';
$b = 'b';
$a .= $b .= "foo";
echo "The value of variable a is: $a<br />";
echo "The value of variable b is: $b<br />";
?>
</p>
</body>
</html>

View the effect

Here is an example.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Arithmetic Operators</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<h2>Auto Parts</h2>
<b>Your order is as follows:</b>
<p>
<?php
$tireqty = 2;
$oilqty = 3;
$sparkqty = 3;
define('TIREPRICE', 50);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
define('SALESTAX', 8.25);

$totalqty = $tireqty + $oilqty + $sparkqty;

$total = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;

echo "$tireqty tires<br />";
echo "$oilqty bottles of oil<br />";
echo "$sparkqty spark plugs<br />";
echo "Items ordered: $totalqty<br />";
echo "Subtotal: \$$total<br />";

$saletax = SALESTAX;
$saletax /= 100; // Divide the sales tax by 100
$tax = $total * $saletax; // Calculate the sales tax
$saletax += 1; // Add 1 to the sales tax
$total *= $saletax; // Calculate the total

echo "Tax: \$$tax<br />";
echo "Total: \$$total";
?>

</p>
</body>
</html>

View the effect


Operators | Arithmetic | Increment/Decrement | Assignment | Comparison | Logical | Execution | String | Bitwise | Ternary | Operator Precedence
© 2008: Hann So
email: hso@voyager.deanza.edu