Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - String Functions
What will be the output of this PHP code?
$text = "  Hello World!  ";
echo strlen(rtrim($text));
A13
B15
C12
D14
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the original string length

    The string " Hello World! " has 16 characters including spaces (2 spaces left, 12 characters in "Hello World!", 2 spaces right).
  2. Step 2: Apply rtrim() and measure length

    rtrim() removes spaces from the right only, so 2 spaces on the right are removed. The length becomes 16 - 2 = 14 characters.
  3. Step 3: Confirm the output

    The code outputs 14.
  4. Final Answer:

    14 -> Option D
  5. Quick Check:

    rtrim() removes right spaces, length = 14 [OK]
Quick Trick: rtrim() removes right spaces, length decreases accordingly [OK]
Common Mistakes:
  • Counting total length without trimming
  • Assuming trim() was used instead of rtrim()
  • Miscounting spaces or characters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes