0
0
PHPprogramming~15 mins

Assignment and compound assignment in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Assignment and Compound Assignment in PHP
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print($stock); to show the current stock.