📖 Scenario: Imagine you are running a small shop. You want to calculate the total price of items bought by a customer, apply discounts, and check if the customer has enough money to pay.
🎯 Goal: You will create a simple PHP program that uses operators to calculate totals, apply discounts, and compare values to decide if the customer can pay.
📋 What You'll Learn
Create variables with exact values for item prices and quantities
Use arithmetic operators to calculate total cost
Use comparison operators to check if payment is enough
Print the final message showing the total and payment status
💡 Why This Matters
🌍 Real World
Operators are used in everyday calculations like shopping, banking, and budgeting to add, subtract, compare, and make decisions.
💼 Career
Understanding operators is essential for any programming job because they help manipulate data and control program flow.
Progress0 / 4 steps
1
DATA SETUP: Create item prices and quantities
Create three variables: $priceApple with value 2, $priceBanana with value 1, and $quantityApple with value 3.
PHP
Hint
Use the = operator to assign values to variables.
2
CONFIGURATION: Add quantity for bananas
Create a variable called $quantityBanana and set it to 5.
PHP
Hint
Remember to use = to assign the value.
3
CORE LOGIC: Calculate total cost and apply discount
Create a variable $total that calculates the total cost by multiplying prices and quantities for apples and bananas, then subtract 3 as a discount.
PHP
Hint
Use parentheses to group calculations and * for multiplication.
4
OUTPUT: Check payment and print result
Create a variable $payment with value 10. Use an if statement to check if $payment is greater than or equal to $total. Print "Payment accepted. Change: " followed by the change amount if enough, otherwise print "Insufficient payment.".
PHP
Hint
Use if, else, and comparison operator >=. Use print() to show messages.
Practice
(1/5)
1. What does the == operator do in PHP?
easy
A. Checks if two values are identical in type and value
B. Assigns a value to a variable
C. Adds two numbers together
D. Checks if two values are equal in value
Solution
Step 1: Understand the == operator
The == operator compares two values to see if they are equal, ignoring type differences.
Step 2: Differentiate from other operators
The = operator assigns values, + adds numbers, and === checks both value and type.
Final Answer:
Checks if two values are equal in value -> Option D
Quick Check:
== means equal value [OK]
Hint: Remember: == compares values, not types [OK]
Common Mistakes:
Confusing == with = (assignment)
Thinking == checks type too
Mixing == with ===
2. Which of the following is the correct way to increment a variable $count by 1 in PHP?
easy
A. $count += 1;
B. $count = 1+;
C. $count =+ 1;
D. $count ++=;
Solution
Step 1: Identify the correct increment syntax
The operator += adds a value to the variable and assigns the result back.
Step 2: Check each option's syntax
$count =+ 1; uses =+ which is invalid. $count = 1+; has misplaced plus sign. $count ++=; is invalid syntax. Only $count += 1; is correct.