Bird
0
0

Find the bug in this PHP code:

medium📝 Debug Q7 of 15
PHP - String Functions
Find the bug in this PHP code:
$result = substr_compare('HelloWorld', 'HelloThere', 0, 5, true);
echo $result;
AThe length parameter is missing
BThe offset parameter should be negative
CNo error, code runs fine
DThe case_insensitivity parameter should be false
Step-by-Step Solution
Solution:
  1. Step 1: Review substr_compare() parameters

    substr_compare(main_str, str, offset, length, case_insensitive) where case_insensitive is a boolean (true for case-insensitive).
  2. Step 2: Validate the call

    All parameters provided correctly: 'HelloWorld', 'HelloThere', offset=0, length=5, case_insensitive=true. Code runs fine.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. Quick Check:

    Valid substr_compare() usage with case_insensitive=true [OK]
Quick Trick: substr_compare() accepts case_insensitive boolean as fifth parameter [OK]
Common Mistakes:
  • Thinking substr_compare() does not accept a boolean for case sensitivity
  • Believing offset must be negative
  • Assuming length parameter is missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes