String length and counting in PHP - Time & Space Complexity
We want to understand how the time to count characters in a string grows as the string gets longer.
How does the program's work increase when the string size increases?
Analyze the time complexity of the following code snippet.
$string = "hello world";
$count = 0;
for ($i = 0; $i < strlen($string); $i++) {
if ($string[$i] === 'l') {
$count++;
}
}
echo $count;
This code counts how many times the letter 'l' appears in the string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the string.
- How many times: Once for every character in the string.
As the string gets longer, the program checks each character one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the string length.
Time Complexity: O(n)
This means the time to count characters grows in a straight line as the string gets longer.
[X] Wrong: "Counting characters takes the same time no matter the string length."
[OK] Correct: The program must look at each character, so longer strings take more time.
Understanding how counting characters scales helps you explain how programs handle text efficiently.
"What if we used a function that counts characters without looping explicitly? How would the time complexity change?"
Practice
strlen() do?Solution
Step 1: Understand the purpose of
Thestrlen()strlen()function counts all characters in a string, including letters, spaces, and symbols.Step 2: Compare with other options
Other options describe different string functions, notstrlen().Final Answer:
It returns the number of characters in a string. -> Option DQuick Check:
strlen()counts characters [OK]
- Thinking strlen() changes the string
- Confusing strlen() with string splitting
- Assuming it removes spaces
$text?Solution
Step 1: Identify the correct function for string length
In PHP,strlen()is the built-in function to get string length.Step 2: Check syntax correctness
strlen($text);correctly calls the function with the variable.Final Answer:
strlen($text); -> Option AQuick Check:
Use strlen() with parentheses and variable [OK]
- Using length() which is not a PHP function
- Using count() which is for arrays
- Forgetting parentheses
$str = "Hello World!";
echo strlen($str);
Solution
Step 1: Count characters in the string including spaces and punctuation
The string "Hello World!" has 12 characters: 5 letters + 1 space + 5 letters + 1 exclamation mark.Step 2: Confirm strlen() counts all characters
strlen($str)returns 12 because it counts every character.Final Answer:
12 -> Option AQuick Check:
Count all characters including spaces and punctuation [OK]
- Ignoring spaces in count
- Forgetting punctuation counts
- Assuming strlen() excludes special characters
$text = "PHP Rocks!";
echo strlen $text;
Solution
Step 1: Check function call syntax
In PHP, functions require parentheses around their arguments. Here,strlenis called without parentheses.Step 2: Verify other parts of the code
The variable name and string are correct, and semicolon is present after echo statement.Final Answer:
strlen() function call missing parentheses -> Option BQuick Check:
Always use parentheses when calling functions [OK]
- Omitting parentheses in function calls
- Confusing variable names
- Missing semicolon after statements
$input is exactly 8 characters long. Which PHP code correctly performs this check?Solution
Step 1: Use strlen() to get string length
The functionstrlen($input)returns the number of characters in the input string.Step 2: Compare length to 8 exactly
The conditionstrlen($input) == 8checks if the length is exactly 8 characters.Step 3: Understand other options
if (count($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; } uses count() which is for arrays, not strings. if (strlen($input) > 8) { echo "Valid length"; } else { echo "Invalid length"; } checks if length is greater than 8, not equal. if (strlen($input) != 8) { echo "Valid length"; } else { echo "Invalid length"; } reverses logic.Final Answer:
if (strlen($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; } -> Option CQuick Check:
Use strlen() == 8 to check exact length [OK]
- Using count() on strings
- Using > or != instead of == for exact match
- Confusing string length with array size
Ready to prove it?
Pass the test and this pattern moves unknown
