linuxStat PHP Documentation
PHP Assignment Statement
An assignment statement assigns an expression to a variable.
For example,
$marketValue   =   $sharePrice   *   $shareCount;
is an assignment statement. It assigns to the variable $marketValue
the market value of Intel (INTC) shares in my Fidelity Investments
broker account.
The market value is computed by multiplying
the current price per share
($sharePrice) by
the number of shares ($shareCount).
Since a PHP variable is one of four types, it is instructive to discuss
the
assignment statement for each variable type separately.
Assignment to integer variables
An integer variable is assigned an integer expression. An example:
$x = 3 + 6 * (61 - 45);
This statement evaluates the right hand side then assigns the value
of the right
hand side (99) to $x.
An integer variable can be assigned to another integer variable. An example:
$x = $y;
This assignment statement assigns the value of $y to $x.
Another example:
$i = $i;
This assignment statement assigns the value of $i to $i. This assignment
does not do any useful work, but it is valid PHP code.
Consider another example:
$i = $i + 1;
This assignment statement takes the current value of $i, adds 1 to it,
then assigns the result to $i. Thus, the above statement increments
the value of $i by 1.
PHP has a shorthand for two commonly used integer assignments.
$i = $i + 1;
is represented in shorthand
$i++;
And
$i = $i - 1;
is represented in shorthand
$i--;
The ++ and --
operators have additional significance
when they appear inside
an integer expression.
The following examples explain their significance.
$i = 3; // Line 1
$j = ++$i; // Line 2
$k = $i++; // Line 3
$l = --$i; // Line 4
$m = $i--; // Line 5
After Line 1 is executed the integer variable $i has the value 3.
In Line 2 the expression on the right side of the = symbol
increments
$i by 1. Thus, $i has the value 4 after the increment. Then the value 4
is assigned to the integer variable $j. Thus, $j gets the value 4.
Line 3 first assigns the value of $i to $k Thus, $k gets the value 4.
Then, Line 3 increments $i by 1. Thus, $i gets the value 5.
Line 4 decrements the value of $i by 1. Thus, $i gets the value 4. Then,
the value of $i is assigned to $l. Thus, $l gets the value 4.
Line 5 assigns the value of $i to $m. Thus, $m gets the value 4.
Then, Line 5 decrements $i by 1. Thus, $i gets the value 3.
What happens first, increment/decrement or assignment,
depends on whether ++/-- appears before or after $i.
You can execute the following PHP program to verify what I said.
I have given a link that you can click to execute the program on my computer.
An example PHP program embedded in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<font size="5">
<?php
# Example of how ++ and -- work.
$i = 3; // Line 1
print "After Line 1 the value of \$i is $i <br>";
$j = ++$i; // Line 2
print "After Line 2 \$j is $j, and \$i is $i <br>";
$k = $i++; // Line 3
print "After Line 3 \$k is $k, and \$i is $i <br>";
$l = --$i; // Line 4
print "After Line 4 \$l is $l, and \$i is $i <br>";
$m = $i--; // Line 5
print "After Line 5 \$m is $m, and \$i is $i <br>";
?>
</font>
</body>
</html>
Click the following link to execute the above PHP program
by my server's PHP interpreter.
Try the above example
There is another important shorthand with respect to integer variable
assignment. The following examples explain this shorthand.
$i = $i + 3 * 7;
is represented in shorthand
$i += 3 * 7;
And
$i = $i - 9;
is represented in shorthand
$i -= 9;
In general
$i = $i op expression;
is represented in shorthand
$i op= expression;
An example PHP program embedded in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<font size="5">
<?php
# Example of shorthand notation for integer assignment
$i = 2; // Line 1
print "After Line 1 the value of \$i is $i <br>";
$i += 1 + 3 * 7; // Line 2
print "After Line 2 \$i is $i <br>";
$i -= 2 * 7; // Line 3
print "After Line 3 \$i is $i <br>";
$i *= 1 + 2 * 2; // Line 4
print "After Line 4 \$i is $i <br>";
$i /= 1 + 1; // Line 5
print "After Line 5 \$i is $i <br>";
?>
</font>
</body>
</html>
Click the following link to execute the above PHP program
by my server's PHP interpreter.
Try the above example
Next
Index