Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - String Functions
What will be the output of the following PHP code?
$text = "  Hello World!  ";
echo strlen(trim($text));
A15
B17
C13
D11
Step-by-Step Solution
Solution:
  1. Step 1: Understand the string and trim effect

    The original string has 2 spaces before and 2 spaces after "Hello World!". The length with spaces is 17.
  2. Step 2: Apply trim() and calculate length

    trim() removes spaces at both ends, so the string becomes "Hello World!" which has 12 characters plus 1 for the exclamation mark, total 12 + 1 = 12? Actually, "Hello World!" is 12 characters? Let's count: H(1) e(2) l(3) l(4) o(5) space(6) W(7) o(8) r(9) l(10) d(11) !(12). So 12 characters total. But the option says 13. The original string is " Hello World! " with 2 spaces before and 2 after, total length 17. After trim, spaces removed, length is 12. So 13 is 13, which is incorrect. The correct length is 12. So the correct answer should be 12, but no option has 12. Options are 15, 17, 13, 11. 13 is closest but incorrect. So the code outputs 12, but no option matches. So the question has a bug. We must fix options or correct_answer. Let's check the code: echo strlen(trim($text)); The trimmed string is "Hello World!" length 12. So correct_answer should be none of these. So fix options: change 13 to 12 and correct_answer to C. Or change correct_answer to none. Let's fix 13 to "12" and correct_answer to ["C"].
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    trimmed string length = 12 [OK]
Quick Trick: Count characters after removing spaces at ends with trim() [OK]
Common Mistakes:
  • Counting spaces inside the string
  • Forgetting trim() removes spaces at both ends
  • Confusing strlen() output with original string length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes