📖 Scenario: Imagine you are calculating the total cost of items in a shopping cart with discounts and taxes applied. Understanding how PHP evaluates expressions with different operators helps you get the correct total.
🎯 Goal: You will write PHP code that uses variables and arithmetic operators. You will learn how operator precedence affects the calculation result.
📋 What You'll Learn
Create variables with exact values for item price, quantity, discount rate, and tax rate.
Create a variable for the discount threshold.
Calculate the total price applying discount only if quantity is above the threshold, then add tax.
Print the final total price.
💡 Why This Matters
🌍 Real World
Calculating prices with discounts and taxes is common in online shopping carts and billing systems.
💼 Career
Understanding operator precedence helps developers avoid bugs in financial calculations and write clear, correct code.
Progress0 / 4 steps
1
DATA SETUP: Create variables for item price, quantity, discount rate, and tax rate
Create four variables with these exact names and values: $itemPrice = 50;, $quantity = 3;, $discountRate = 0.1;, and $taxRate = 0.07;
PHP
Hint
Use the $ sign for variable names and assign the exact values given.
2
CONFIGURATION: Create a discount threshold variable
Create a variable called $discountThreshold and set it to 2
PHP
Hint
Remember to use $discountThreshold as the variable name and assign the value 2.
3
CORE LOGIC: Calculate total price with discount and tax using operator precedence
Create a variable called $totalPrice that calculates the total price as follows: multiply $itemPrice by $quantity, then subtract the discount only if $quantity is greater than $discountThreshold (discount is $itemPrice * $quantity * $discountRate), then add tax on the discounted amount (tax is $totalPrice * $taxRate). Use parentheses to ensure correct operator precedence.
PHP
Hint
Use the ternary operator condition ? true_value : false_value to apply discount only if quantity is above threshold. Use parentheses to control the order of operations.
4
OUTPUT: Print the final total price
Write a print statement to display the value of $totalPrice.
PHP
Hint
Use print($totalPrice); to show the final result.
Practice
(1/5)
1. In PHP, which operator has the highest precedence among these: +, *, -, /?
easy
A. All have the same precedence
B. Addition (+)
C. Subtraction (-)
D. Multiplication (*) and division (/)
Solution
Step 1: Recall operator precedence rules
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
Step 2: Compare given operators
Among +, -, *, /, the * and / operators are evaluated first.
Final Answer:
Multiplication (*) and division (/) -> Option D
Quick Check:
Higher precedence = * and / [OK]
Hint: Remember: multiply/divide before add/subtract [OK]
Common Mistakes:
Thinking addition has higher precedence
Assuming all operators have equal precedence
Confusing subtraction with division precedence
2. Which of the following PHP expressions is syntactically correct and respects operator precedence?
easy
A. $result = 5 + 3 * 2;
B. $result = 5 + * 3 2;
C. $result = 5 + (3 * 2;
D. $result = 5 + 3 *;
Solution
Step 1: Check syntax of each expression
$result = 5 + 3 * 2; is a valid PHP expression with correct operator placement. Options B, C, and D have syntax errors like misplaced operators or missing parentheses.