Bird
0
0

Identify the error in the following PHP code that tries to remove trailing spaces:

medium📝 Debug Q14 of 15
PHP - String Functions
Identify the error in the following PHP code that tries to remove trailing spaces:
$str = "Test string   ";
$newStr = ltrim($str);
echo "'$newStr'";
Altrim() removes spaces from the left, not the right
BMissing semicolon after echo statement
CUsing single quotes inside echo causes error
DVariable $newStr is not defined
Step-by-Step Solution
Solution:
  1. Step 1: Understand what ltrim() does

    ltrim() removes spaces from the left side of the string, but the spaces are on the right side.
  2. Step 2: Check code for other errors

    Semicolons are correct, single quotes inside echo are valid, and $newStr is defined properly.
  3. Final Answer:

    ltrim() removes spaces from the left, not the right -> Option A
  4. Quick Check:

    Use rtrim() to remove right side spaces [OK]
Quick Trick: Use rtrim() for right side spaces, ltrim() for left [OK]
Common Mistakes:
  • Using ltrim() to remove right side spaces
  • Thinking single quotes cause errors in echo
  • Assuming variable is undefined without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes