PHP Assignment Operators - PHP Operators - php frameworks

PHP Assignment Operators

/ins>

Assignment Operators in PHP

There are a few other Operators that tend to do some arithmetic operations
and store the result in same. for eg the addition-assignment operator, represented
by the symbol +=, lets you simultaneously add and assign a new value to a variable.

Common Assignment Operators
Operatos Description
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign quotient
%= Divide and assign modulus
.= Concatenate and assign(its used only for sting)

and here’s example illustrating these operators in action.

Add and assign

Output
sum=1000

In the above example
Initialize variable ($x) with value = 500. If we want to add 500 to this value . we don’t need a second and third variable to store the sum of value($x+=500) it means ($x=$x+500 ) .
add 500 and re-assign new value(1000) back to same variable ($x).


Subtract and assign

Output
subtraction = 500

In the above example
Create and initialize variable($x) hold value = 1000. now perform subtraction. 500 is subtract from 1000 using($x-=500) it means($x=$x – 500 ) .
Now assign new value back to the initialize variable($x). so the output will become:


Multiply and assign

Output
Multiplication= 1000

In the above example
Variable( $x) with value=100. now perform multiplication. ($x*=10) now value 10 multiply with previous value(100).
and the output will become:1000 and it re-assign back to the variable ($x)
so the output will become : 1000


Divide and assign quotient

Output
Quotient = 2

In the above example.
Declare Variable( $x) with value=1000. now perform divide.($x/=500) now value 500 divide with previous value(1000).
and the output will become:2 and it re-assign value=2, back to the variable ($x).


Divide and assign modulus

Output
Remainder= 1

In the above example.
Variable($x) with value =5. Now calculate the modulus using ($x%=2) .
it gives remainder value=”1″ and this remainder assign again to variable($x).
and the output will become : 1


Concatenate and assign

Output
Welcome to the world of phptpoint

In the above example.
Declare variable($str). With string value=”Welcome” .
Now concatenate this string to another string using the same variable
by performing ($str.=”to the world of php”).
It concatenate “welcome” with “to the world of php” and
The output will become this: Welcome to the world of Phptpoint