String concatenation operator in PHP - Time & Space Complexity
When we join strings together in PHP using the concatenation operator, it takes some time depending on the size of the strings.
We want to understand how the time to join strings grows as the strings get longer or as we join more strings.
Analyze the time complexity of the following code snippet.
$string = "";
for ($i = 0; $i < $n; $i++) {
$string .= "a";
}
This code builds a string by adding the letter "a" one by one, repeating $n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding one character to the string using concatenation inside a loop.
- How many times: The concatenation happens once per loop, so $n times.
Each time we add a character, PHP creates a new string by copying the old string and adding the new character.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 (1+2+...+10) |
| 100 | About 5,050 |
| 1000 | About 500,500 |
Pattern observation: The total work grows roughly like the square of n because each concatenation copies the whole string built so far.
Time Complexity: O(n²)
This means the time to build the string grows roughly with the square of the number of characters added.
[X] Wrong: "Concatenating strings in a loop is always fast and takes time proportional to n."
[OK] Correct: Each concatenation copies the entire string so far, making the total time grow much faster than just n.
Understanding how string joining works helps you write efficient code and explain your choices clearly in real projects or interviews.
"What if we used an array to collect characters and joined them once at the end? How would the time complexity change?"
Practice
.) operator do in PHP?Solution
Step 1: Understand the dot operator usage
In PHP, the dot operator is used to join or concatenate strings.Step 2: Differentiate from other operators
It does not perform arithmetic or comparison; it only combines strings.Final Answer:
Joins two strings together -> Option AQuick Check:
Dot operator = string join [OK]
- Confusing dot with plus (+) for addition
- Thinking dot compares strings
- Using dot for division
$a and $b in PHP?Solution
Step 1: Identify the concatenation operator
PHP uses the dot (.) operator to join strings, so $a . $b is correct.Step 2: Check other operators
Plus (+) adds numbers, ampersand (&) is bitwise AND, and asterisk (*) is multiplication.Final Answer:
$a . $b -> Option BQuick Check:
Dot (.) concatenates strings [OK]
- Using + instead of . for strings
- Using & or * operators mistakenly
- Missing the dot operator
$greeting = "Hello"; $name = "Alice"; echo $greeting . ", " . $name . "!";
Solution
Step 1: Analyze string concatenation
The code joins "Hello", ", ", "Alice", and "!" using the dot operator.Step 2: Combine strings step-by-step
"Hello" . ", " = "Hello, " and then "Hello, " . "Alice" = "Hello, Alice" and finally "Hello, Alice" . "!" = "Hello, Alice!"Final Answer:
Hello, Alice! -> Option CQuick Check:
Concatenate with dot = Hello, Alice! [OK]
- Missing spaces inside strings
- Expecting plus (+) to work
- Confusing output with literal dots
$first = "Good"; $second = "Morning"; echo $first + " " + $second;
Solution
Step 1: Identify operator misuse
The code uses + to join strings, but PHP requires the dot (.) operator for concatenation.Step 2: Confirm other syntax is correct
Semicolons are present, variables declared, and quotes are correct.Final Answer:
Using + instead of . for string concatenation -> Option DQuick Check:
Use dot (.) not plus (+) for strings [OK]
- Using + to join strings
- Thinking + works like in JavaScript
- Ignoring PHP string rules
$firstName and $lastName with a space between them. Which code correctly does this in PHP?Solution
Step 1: Understand string concatenation with space
To join first and last names with a space, you must add " " explicitly between them using dot operators.Step 2: Evaluate each option
A misses the space, B uses + which is invalid for strings, D uses single quotes so variables are not interpolated.Final Answer:
$fullName = $firstName . " " . $lastName; -> Option AQuick Check:
Dot with space string joins names correctly [OK]
- Forgetting space between names
- Using + instead of .
- Assuming variables parse inside single quotes
Ready to prove it?
Pass the test and this pattern moves unknown
