📖 Scenario: You are managing a small online store's inventory. You need to keep track of the number of items in stock and update the stock as sales happen.
🎯 Goal: Build a simple PHP script that uses assignment and compound assignment operators to update the stock count of a product.
📋 What You'll Learn
Create a variable to hold the initial stock count.
Create a variable to hold the number of items sold.
Use a compound assignment operator to update the stock count after sales.
Print the updated stock count.
💡 Why This Matters
🌍 Real World
Managing inventory counts in an online store or any business that tracks stock.
💼 Career
Understanding assignment and compound assignment is essential for updating values efficiently in programming tasks such as inventory management, financial calculations, and data processing.
Progress0 / 4 steps
1
Create the initial stock count variable
Create a variable called $stock and set it to 50 to represent the initial number of items in stock.
PHP
Hint
Use the assignment operator = to set the value.
2
Create the items sold variable
Create a variable called $sold and set it to 13 to represent the number of items sold.
PHP
Hint
Remember to use the assignment operator =.
3
Update the stock using compound assignment
Use the compound assignment operator -= with the variable $stock and the variable $sold to subtract the sold items from the stock.
PHP
Hint
The -= operator subtracts the right value from the left variable and assigns the result back to the left variable.
4
Print the updated stock count
Write a print statement to display the updated value of $stock.
PHP
Hint
Use print($stock); to show the current stock.
Practice
(1/5)
1. What does the += operator do in PHP?
easy
A. Assigns the right value to the left variable without any operation
B. Multiplies the left variable by the right value
C. Subtracts the right value from the left variable
D. Adds the right value to the left variable and assigns the result to the left variable
Solution
Step 1: Understand the += operator
The += operator adds the value on the right to the variable on the left.
Step 2: Assign the result back to the left variable
It updates the left variable with the new sum.
Final Answer:
Adds the right value to the left variable and assigns the result to the left variable -> Option D
Quick Check:
+= means add and assign [OK]
Hint: Think: add right to left and save in left [OK]
Common Mistakes:
Confusing += with simple assignment =
Thinking it subtracts or multiplies
Believing it only adds without assignment
2. Which of the following is the correct syntax to multiply a variable $x by 5 and update it in PHP?
easy
A. $x *= 5;
B. $x =* 5;
C. $x = *5;
D. $x = 5*;
Solution
Step 1: Identify the correct compound assignment syntax
The correct syntax for multiplying and assigning is $x *= 5; with no spaces between * and =.
Step 2: Check other options for syntax errors
The other options have incorrect placement of operators or missing operands.
Final Answer:
$x *= 5; -> Option A
Quick Check:
Use *= for multiply and assign [OK]
Hint: Remember no space: variable *= value [OK]
Common Mistakes:
Writing =* instead of *= operator
Adding spaces between * and =
Placing * or = incorrectly
3. What will be the output of the following PHP code?