Assignment operators in PHP are used to assign values to variables and update those values easily. You may already know the basic assignment operator (=), but PHP also provides operators such as +=, -=, *=, /=, and .= to simplify calculations and string operations. In this chapter, you will practice PHP assignment operator questions with solutions and outputs to strengthen your programming basics. PHP Assignment Operators Practice Questions with Solutions to help you understand the concepts.
Question 1: Assign a Value to a Variable Using the = Operator
Problem Statement: Write a PHP program to assign a student’s name, age, and marks to variables and display their values.
PHP Solution:
<?php
$name = "Rahul";
$age = 20;
$marks = 85;
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Marks: " . $marks;
?>
Sample Output:
Name: Rahul
Age: 20
Marks: 85
Explanation: The = operator assigns a value to a variable. Here, the student’s name, age, and marks are stored in three separate variables and displayed using echo.
Concepts Covered: =, variables, strings, integers, and output.
Question 2: Increase a Number Using the += Operator
Problem Statement: Write a PHP program that starts with a balance of ₹5,000 and adds ₹2,000 to it using the += operator.
PHP Solution:
<?php
$balance = 5000;
$balance += 2000;
echo "Updated Balance: Rs. " . $balance;
?>
Sample Output:
Updated Balance: Rs. 7000
Explanation: The += operator adds a value to the variable’s existing value and stores the result in the same variable.
$balance += 2000; is equivalent to $balance = $balance + 2000;.
Concepts Covered: +=, addition, and updating variable values.
Question 3: Subtract an Amount Using the -= Operator
Problem Statement: A customer has ₹10,000 in their account and withdraws ₹2,500. Write a PHP program to calculate the remaining balance using -=.
PHP Solution:
<?php
$balance = 10000;
$balance -= 2500;
echo "Remaining Balance: Rs. " . $balance;
?>
Sample Output:
Remaining Balance: Rs. 7500
Explanation: The -= operator subtracts the specified amount from the current variable value and saves the result back into that variable.
Concepts Covered: -=, subtraction, and balance calculations.
Question 4: Multiply a Value Using the *= Operator
Problem Statement: A shop sells 5 notebooks at ₹40 each. Write a PHP program to calculate the total cost using the *= operator.
PHP Solution:
<?php
$total = 5;
$total *= 40;
echo "Total Cost: Rs. " . $total;
?>
Sample Output:
Total Cost: Rs. 200
Explanation: The *= operator multiplies the existing variable value by another number and assigns the result back to the variable.
Concepts Covered: *=, multiplication, and calculating total cost.
Question 5: Divide a Value Using the /= Operator
Problem Statement: A company distributes a bonus of ₹24,000 equally among 4 employees. Write a PHP program to calculate each employee’s share using /=.
PHP Solution:
<?php
$bonus = 24000;
$bonus /= 4;
echo "Bonus Per Employee: Rs. " . $bonus;
?>
Sample Output:
Bonus Per Employee: Rs. 6000
Explanation: The /= operator divides the current variable value by another value and stores the result in the same variable. The divisor must not be zero.
Concepts Covered: /=, division, and distributing amounts.
Question 6: Find the Remainder Using the %= Operator
Problem Statement: Write a PHP program that starts with 27 and uses %= to find the remainder after dividing the number by 4.
PHP Solution:
<?php
$number = 27;
$number %= 4;
echo "Remainder: " . $number;
?>
Sample Output:
Remainder: 3
Explanation: The %= operator calculates the remainder and assigns it back to the variable.
27 % 4 produces 3 because 4 goes into 27 six times, leaving a remainder of 3.
Concepts Covered: %=, modulus, and remainder calculations.
Question 7: Calculate a Power Using the **= Operator
Problem Statement: Write a PHP program to calculate the square of a number using the exponentiation assignment operator **=.
PHP Solution:
<?php
$number = 6;
$number **= 2;
echo "Square: " . $number;
?>
Sample Output:
Square: 36
Explanation: The **= operator raises the variable’s current value to a specified power and assigns the result back to it.
$number **= 2; is equivalent to $number = $number ** 2;.
Concepts Covered: **=, exponentiation, and mathematical calculations.
Question 8: Join Strings Using the .= Operator
Problem Statement: Write a PHP program to build a student’s full name by joining the first name and last name using the .= operator.
PHP Solution:
<?php
$fullName = "Amit";
$fullName .= " Sharma";
echo "Full Name: " . $fullName;
?>
Sample Output:
Full Name: Amit Sharma
Explanation: The .= operator appends a string to the existing string stored in a variable.
$fullName .= " Sharma"; is equivalent to $fullName = $fullName . " Sharma";.
Concepts Covered: .= , string concatenation, and string variables.
Question 9: Update a Shopping Bill Using Multiple Assignment Operators
Problem Statement: A customer buys products worth ₹2,000, receives a discount of ₹300, and then has a delivery charge of ₹100 added. Write a PHP program using assignment operators to calculate the final bill.
PHP Solution:
<?php
$bill = 2000;
$bill -= 300;
$bill += 100;
echo "Final Bill: Rs. " . $bill;
?>
Sample Output:
Final Bill: Rs. 1800
Explanation: The program first subtracts the discount from the bill and then adds the delivery charge. Each assignment operator updates the existing value of $bill.
Concepts Covered: -=, +=, arithmetic calculations, and multiple assignments.
Question 10: Update a Variable Using the ??= Operator
Problem Statement: Write a PHP program that assigns a default username if the variable is not already set or has a null value.
PHP Solution:
<?php
$username = null;
$username ??= "Guest";
echo "Username: " . $username;
?>
Sample Output:
Username: Guest
Explanation: The ??= operator assigns a default value only when the variable is undefined or its value is null. If the variable already contains a non-null value, it keeps that value.
Note: The ??= operator is available in PHP 7.4 and later.
Concepts Covered: ??=, null values, default assignments, and variable handling.
Key Takeaways
=assigns a value to a variable.+=adds a value to the existing variable value.-=subtracts a value from the existing variable value.*=multiplies a variable by another value./=divides a variable by another value.%=stores the remainder of a division.**=performs exponentiation and stores the result..=appends text to an existing string.??=assigns a default value when a variable is undefined ornull.- Assignment operators make code shorter and easier to maintain.
Frequently Asked Questions
1. What are assignment operators in PHP?
Assignment operators assign values to variables or update the values already stored in them.
2. What is the difference between = and == in PHP?
The = operator assigns a value, while == compares two values for equality.
3. What does the += operator do in PHP?
It adds a value to the existing variable value and stores the result in that variable.
4. What is the purpose of the -= operator?
It subtracts a specified value from the current variable value and saves the result.
5. What does the .= operator mean in PHP?
The .= operator appends one string to another string stored in a variable.
6. Can assignment operators be used with strings?
Yes. The .= operator is commonly used to append text to an existing string.
7. What does the %= operator calculate?
It calculates the remainder after division and assigns that remainder to the variable.
8. What is the **= operator used for?
It raises a number to a specified power and stores the result in the same variable.
9. What happens if you use /= with zero as the divisor?
Division by zero is invalid and can cause an error or exception in PHP. Always check that the divisor is not zero.
10. What is the ??= operator in PHP?
The ??= operator assigns a default value when a variable is undefined or null. It was introduced in PHP 7.4.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
