Arithmetic operators in PHP - Time & Space Complexity
When using arithmetic operators in PHP, it's helpful to know how the time to run your code changes as you work with bigger numbers or more calculations.
We want to see how the number of operations grows when using arithmetic operators repeatedly.
Analyze the time complexity of the following code snippet.
$sum = 0;
for ($i = 1; $i <= $n; $i++) {
$sum += $i; // addition operation
}
echo $sum;
This code adds numbers from 1 up to n using the addition operator inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Addition inside the loop ($sum += $i)
- How many times: Exactly n times, once for each number from 1 to n
As n grows, the number of additions grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to finish the additions grows in a straight line as the input size increases.
[X] Wrong: "Arithmetic operations like addition take constant time no matter how many times they run, so the whole loop is constant time."
[OK] Correct: While each addition is quick, doing it n times means the total time grows with n, so the loop is not constant time.
Understanding how simple arithmetic inside loops affects time helps you explain how your code scales, a key skill in many programming discussions.
"What if we replaced the addition inside the loop with a multiplication? How would the time complexity change?"
Practice
Solution
Step 1: Understand the modulus operator and compare with other operators
The modulus operator (%) returns the remainder after division of two numbers. Division (/) returns the quotient, multiplication (*) multiplies, and subtraction (-) subtracts.Final Answer:
The modulus operator (%) -> Option BQuick Check:
Remainder = % [OK]
- Confusing division (/) with modulus (%)
- Using multiplication (*) instead of modulus
- Thinking subtraction (-) gives remainder
$a and $b in PHP?Solution
Step 1: Identify the addition operator and check the syntax correctness
In PHP, the plus sign (+) adds two numbers or variables. The syntax$sum = $a + $b;correctly assigns the sum of $a and $b to $sum.Final Answer:
$sum = $a + $b; -> Option DQuick Check:
Addition uses + [OK]
- Using - instead of + for addition
- Using * or / which are multiplication/division
- Missing semicolon at end
$x = 10;
$y = 3;
echo $x % $y;
Solution
Step 1: Calculate the modulus of 10 and 3 and understand the output of echo $x % $y;
10 divided by 3 is 3 with a remainder of 1. The modulus operator (%) returns the remainder, so output is 1.Final Answer:
1 -> Option AQuick Check:
10 % 3 = 1 [OK]
- Confusing modulus with division result
- Expecting 3 instead of remainder 1
- Syntax errors from missing semicolon
$a = 5;
$b = 0;
echo $a / $b;
Solution
Step 1: Analyze the division operation and check for syntax correctness
Dividing any number by zero is not allowed and causes an error in PHP. Semicolons are present; multiplication operator is not used here.Final Answer:
Division by zero error -> Option CQuick Check:
Division by zero causes error [OK]
- Ignoring division by zero error
- Thinking output is 0
- Confusing syntax error with runtime error
$price. Which PHP expression correctly calculates the discounted price?Solution
Step 1: Calculate the discount amount and subtract discount from original price
Multiply the price by 0.15 to get 15% of the price. Subtract the discount amount from the original price to get the discounted price.Final Answer:
$discounted = $price - ($price * 0.15); -> Option AQuick Check:
Discounted price = price - 15% price [OK]
- Adding discount instead of subtracting
- Using only 15% value without subtracting
- Dividing price by 0.15 incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
