Page 52 - Open Soource Technologies 304.indd
P. 52
Web Technologies-I
Notes Because all operators are required to return a value, the assignment operator returns the value
assigned to the variable. For example, the expression $a = 5 not only assigns 5 to $a, but also
behaves as the value 5 if used in a larger expression. Consider the following expressions:
$a = 5; $b = 10; $c = ($a = $b);
The expression $a = $b is evaluated first, because of the parentheses. Now, both $a and $b have
the same value, 10. Finally, $c is assigned the result of the expression $a =$b, which is the value
assigned to the left-hand operand (in this case, $a). When the full expression is done evaluating,
all three variables contain the same value, 10.
Assignment with Operation
In addition to the basic assignment operator, there are several assignment operators that are
convenient shorthand. These operators consist of a binary operator followed directly by an equals
sign, and their effect is the same as performing the operation with the operands, then assigning
the resulting value to the left-hand operand. These assignment operators are:
Plus-equals (+=)
Adds the right-hand operand to the value of the left-hand operand, and then assigns the result
to the left-hand operand. $a += 5 is the same as $a = $a + 5.
Minus-equals (–=)
Subtracts the right-hand operand from the value of the left-hand operand, and then assigns the
result to the left-hand operand.
Divide-equals (/=)
Divides the value of the left-hand operand by the right-hand operand, and then assigns the result
to the left-hand operand.
Multiply-equals (*=)
Multiplies the right-hand operand with the value of the left-hand operand, and then assigns the
result to the left-hand operand.
Modulus-equals (%=)
Performs the modulus operation on the value of the left-hand operand and the right-hand operand,
and then assigns the result to the left-hand operand.
Bitwise-XOR-equals (^=)
Performs a bitwise XOR on the left-hand and right-hand operands, then assigns the result to the
left-hand operand.
Bitwise-AND-equals (&=)
Performs a bitwise AND on the value of the left-hand operand and the right-hand operand, then
assigns the result to the left-hand operand.
Bitwise-OR-equals (|=)
Performs a bitwise OR on the value of the left-hand operand and the right-hand operand, and
then assigns the result to the left-hand operand.
Concatenate-equals (. =)
Concatenates the right-hand operand to the value of the left-hand operand, and then assigns the
result to the left-hand operand.
46 LOVELY PROFESSIONAL UNIVERSITY