 Assignment Operators
× Gray Matter
Assignment Operators

In PHP, we have "=" as a basic operator. If we write "\$A=5", it means that operand A is set to value 5. In same way we can do some tricky things by using equal sign and other operator sign together.

### Operator (=)

This is a simple PHP operator which assign the value of the expression on right side to the operand on left side.

``````<?php
\$X = 7;
\$Y = \$X;
echo \$Y;
?>``````

Output:

7

In the above code we set the value of \$Y to 7, the value of \$X.

### Operator (+=)

This operator will first do addition of left and right side operands and later will set the result to left side operand.

See below example-

``````<?php
\$X = 7;
\$X += 20; // This will work like \$X = \$X + 20;
echo \$X;
?>``````

Output:

27

### Operator (-=)

This operator will first do subtraction of left and right side operands and later will set the result to left side operand.

See below example-

``````<?php
\$X = 7;
\$X -= 20; //This will work like \$X = \$X-20;
echo \$X;
?>``````

Output:

-13

### Operator (*=)

This operator will first do multiplication of left and right side operands and later will set the result to left side operand.

See below example-

``````<?php
\$X = 7;
\$X *= 20;
echo \$X;
?>``````

Output:

140

### Operator (/=)

This operator will first divide the left side operand from right side operand and later will set the result to left side operand.

See below example-

``````<?php
\$X = 7;
\$X /= 20; // This will work like \$X = \$X/20;
echo \$X;
?>``````

Output:

0.35

### Operator (%=)

This operator will first divide the left side operand from right side operand and later will set the remainder to left side operand.

See below example-

``````<?php
\$X = 7;
\$X %= 2;
echo \$X;
?>``````

Output:

1

### Operator (.=)

This operator will first divide the left side operand from right side operand and later will set the remainder to left side operand.

See below example-

``````<?php
\$X = "Hello! ";
\$X .= "How are you today?";
echo \$X;
?>``````

Output:

Hello! How are you today?

We can do much more with assignment operators. Visit CodeLAB to prectice with your own code.

← Preview

Increment and Decrement Operators

Next →

Comparison Operators

NOTES Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

## Courses

• Python
• Game Development with Unity 3D
• Arduino
• PCB & Circuit Designing
• Robotics and Embedded C
• 8051/8052 Embedded Systems Get the Android App Get the iOS App