## PHP Assignment Operators

To better understand what is an assignment operator, lets re-think once again how do we declare a variable and set value to it?

Read Article: Variable Declaration in PHP

### Assignment Operator

The symbol of assignment operator is =

Example

<?php

\$num=100;

?>

In the above example, we have declared variable name \$num. It holds the value 100.

Note: In mathematics =  known as ‘is equal to’, but in PHP, = known as assignment operator.

Why?

Because, = sign assigns the value to the variable. As we have already learned from second chapter, variable declaration in real creates memory allocation which is referred by the reference name. The moment when we assign a value using assignment operator, the value is stored at the memory location.

Therefore the point here is, to set a value to variable we use = sign. = sign in PHP known as assignment operator.

Now, there are some more assignment operators exist, lets know about them

The symbol of addition assignment operator is +=

Example

We have \$num=10; and a requirement came to increase the value of the \$num by 10. Then how can we achieve this?

Solution 01

<?php

\$num=10;

\$num=\$num+10;

echo \$num;

?>

Output: 20

Solution 02

Now, the above result can also be achieved through below code

<?php

\$num=10;

\$num+=10;

echo \$num;

?>

Output: 20

In the above example we have used addition assignment operator. (\$num+=10)

This code \$num=\$num+10; is equivalent to this code \$num+=10;

Subtraction Assignment Operator

The symbol for subtraction assignment operator is -=

Example

We have \$num=10; and a requirement came to deduct the value of \$num by 5

Solution

<?php

\$num=10;

\$num-=5; // this code is equivalent to \$num=\$num-5;

echo \$num;

?>

Output:5

Multiplication Assignment Operator

The symbol for multiplication assignment operator is *=

Example

We have \$num=10; a requirement came to increase the value of \$num to 5 times the value stored in it.

Solution

<?php

\$num=10;

\$num*= 5; //this code is equivalent to \$num=\$num*5;

echo \$num;

?>

Output: 50

Division Assignment Operator

The symbol for division assignment operator is /=

Example

We have \$num=10; A requirement came to reduce the value of \$num variable by 5 times.

solution

<?php

\$num=10;

\$num/=5; // This code is equivalent to \$num=\$num/5;

echo \$num;

?>

Output: 2