PHP Assignment Operators

In this chapter, we are going to focus on assignment operators.

What does Assignment Operator do?

The job of assignment operator is to assign value to the variable. In general life, = (is equal to) compares the value (E.g price of chocolate is equal to $1 USD) but in programming world “=” is used to assign a value to a variable.

If you are not aware of what is a variable then read this article: PHP Variable Declaration

Example:

<?php $num=10; ?>

In the above code, $num is a variable name and 10 is the value and we are assigning this value to $num. (At the backend, a memory location is created where value 10 is stored and this memory location is referred by the name $num)

Are there only one type of assignment operator?

No. There are other type of assignment operators too.

Operator Operation Example
= It assigns value $num=100
+= Performs addition first then assigns value $num+=100
-= Performs subtraction first then assigns value $num-=100
*= Performs multiplication first then assigns value $num*=100
/= Performs division first then assigns value $num/=100
%= Performs modulus operation first then assigns value $num%=100

Lets see in detail with examples.

“+=” Operator:

To understand this assignment operators job, lets take one example.

One variable is declared and a value of 100 is assigned to it.

<?php $num1=100; ?>

Now, a requirement came, and I need increment the value of this variable by 500. So I can achieve this in following way.

<?php

$num1=100;

$num2=$num1+500;

echo $num1=$num2; ?>

What I did above?

I added 500 to $num1 variable and then assigned to another variable $num2. Now $num2 variable contains the addition value of $num1 and 500. That is 600. And then I have assigned $num2 to $num1. $num1 which was previously containing 100, now contains 600.

Remember: Our job was to increment the variable value by 500 and in above approach we have achieved the requirement.

Now, the same above result can also be achieved in following way:

<?php

$num=100;

$num=$num+500;

echo $num; ?>

What I did above?

Instead of adding $num and 500, assigning to a new variable $num2 and reassigning back to $num1, I have added $num and 500 and assigned to $num. Now, $num contains addition value of $num and 500 which is 600. But wait, we have easy solution to it.

The same above performance can also be achieved by using += operator as below.

<?php

$num=100;

$num+=500;

echo $num; ?>

Here $num+=500 is same to $num=$num+500.

“-=” Operator:

To better understand -= operator, lets take another example.

I have a variable with a value 500 assigned to it and a requirement came to decrement its value by 300.

<?php

$num=500;

$num-=300;

echo $num ?>

It is similar to += operator but -= operator subtracts value then assigns the value to the variable.

$num-=300 is same as $num=$num-300

“*=” Operator:

To better understand, *= lets take another example.

I have a variable with value 100 assigned to it and a requirement came to increment its value 5 times. I can achieve this using *= operator.

<?php $num=100;

$num*=5;

echo $num; ?>

The above code is going to print 500.

$num*=5 is same as $num=$num*5;

“/=” Operator:

To better understand /= operator, lets take example.

I have a variable assigned a value 100 and a requirement came to decrement its value by 5 times. I can achieve this in following way:

<?php

$num=100;

$num/=5;

echo $num; ?>

The above code is going to print 20.

$num/=5 is same as $num=$num/5;

“%=” Operator:

To better understand %= operator, lets take example.

I have a variable assigned value 15 and a requirement came to assign remainder value to it after dividing by 10. I can achieve this in following way:

<?php

$num=15;

$num%=10;

echo $num;

?>

The above code is going to print 5.

$num%=5 is same as $num=$num%5