Last updated on November 27th, 2023
What are assignment operators in PHP?
Assignment operators in PHP are used to assign values to variables. They allow you to store data in variables for later use in the PHP code. Assignment operators help you manage and manipulate data efficiently in your PHP programs. They make it easy to update variables based on mathematical operations or other data transformations.
What types of assignment operators are there?
There are 5 main types of assignment operators:
1. Assignment Operator (=)
The basic assignment operator (=) is used to assign a value to a variable. It takes the value on the right side of the operator and stores it in the variable on the left side. This operator is fundamental for storing and manipulating data in your PHP scripts.
2. Addition Assignment Operator (+=)
The addition assignment operator (+=) combines addition and assignment. It takes the current value of a variable, adds another value to it, and then stores the result back in the same variable. This is useful for updating variables when you want to add something to their existing values.
3. Subtraction Assignment Operator (-=)
The subtraction assignment operator (-=) combines subtraction and assignment. It takes the current value of a variable, subtracts another value from it, and stores the result back in the same variable. It’s handy for updating variables when you want to subtract something from their current values.
4. Multiplication Assignment Operator (*=)
The multiplication assignment operator (*=) combines multiplication and assignment. It takes the current value of a variable, multiplies it by another value, and stores the result back in the same variable. You can use this operator for updating variables when you want to apply a percentage or scale a value.
5. Division Assignment Operator (/=)
The division assignment operator (/=) combines division and assignment. It takes the current value of a variable, divides it by another value, and stores the result back in the same variable. It’s useful when you need to calculate averages, distribute values, or split something into equal parts.
Examples of using the assignment operators
1. Assignment (=) Operator
<?php $x = 5; echo $x; ?>
The basic assignment operator (=) assigns the value 5 to the variable $x. It sets the variable’s value to the right-hand side value.
2. Addition Assignment (+=) Operator
<?php $total = 10; $bonus = 5; $total += $bonus; echo $total; ?>
The += operator adds the value of $bonus (5) to the value of $total (10) and updates $total to 15.
3. Subtraction Assignment (-=) Operator
<?php $balance = 100; $withdrawal = 25; $balance -= $withdrawal; echo $balance; ?>
The -= operator subtracts the value of $withdrawal (25) from the value of $balance (100) and updates $balance to 75.
4. Multiplication Assignment (*=) Operator
<?php $quantity = 5; $pricePerItem = 10; $totalCost = $quantity * $pricePerItem; $totalCost *= 0.9; echo $totalCost; ?>
The *= operator multiplies the value of $totalCost by 0.9, effectively applying a 10% discount.
5. Division Assignment (/=) Operator
<?php $Amount = 100; $numPeople = 4; $Amount /= $numPeople; echo $Amount; ?>
The /= operator divides the value of $Amount by the value of $numPeople to calculate the amount per person (25).