05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Because all operators are required to return a value, the assignment operator returns<br />

the value assigned to the variable. For example, the expression $a = 5 not only assigns<br />

5 to $a, but also behaves as the value 5 if used in a larger expression. Consider the<br />

following expressions:<br />

$a = 5;<br />

$b = 10;<br />

$c = ($a = $b);<br />

The expression $a=$bis evaluated first, because of the parentheses. Now, both $a and<br />

$b have the same value, 10. Finally, $c is assigned the result of the expression $a = $b,<br />

which is the value assigned to the lefthand operand (in this case, $a). When the full<br />

expression is done evaluating, all three variables contain the same value, 10.<br />

Assignment with operation<br />

In addition to the basic assignment operator, there are several assignment operators<br />

that are convenient shorthand. These operators consist of a binary operator followed<br />

directly by an equals sign, and their effect is the same as performing the operation<br />

with the operands, then assigning the resulting value to the lefthand operand.<br />

These assignment operators are:<br />

Plus-equals (+=)<br />

Adds the righthand operand to the value of the lefthand operand, then assigns<br />

the result to the lefthand operand. $a += 5 is the same as $a=$a+5.<br />

Minus-equals (–=)<br />

Subtracts the righthand operand from the value of the lefthand operand, then<br />

assigns the result to the lefthand operand.<br />

Divide-equals (/=)<br />

Divides the value of the lefthand operand by the righthand operand, then assigns<br />

the result to the lefthand operand.<br />

Multiply-equals (*=)<br />

Multiplies the righthand operand with the value of the lefthand operand, then<br />

assigns the result to the lefthand operand.<br />

Modulus-equals (%=)<br />

Performs the modulus operation on the value of the lefthand operand and the<br />

righthand operand, then assigns the result to the lefthand operand.<br />

Bitwise-XOR-equals (^=)<br />

Performs a bitwise XOR on the lefthand and righthand operands, then assigns<br />

the result to the lefthand operand.<br />

Bitwise-AND-equals (&=)<br />

Performs a bitwise AND on the value of the lefthand operand and the righthand<br />

operand, then assigns the result to the lefthand operand.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Expressions and Operators | 45

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!