Bird
0
0

Why does strpos return false instead of 0 when the substring is not found, and how should you check its result?

hard📝 Conceptual Q10 of 15
PHP - String Functions
Why does strpos return false instead of 0 when the substring is not found, and how should you check its result?
ABecause 0 is a valid position; check with strict !== false to avoid confusion
BBecause 0 means substring found at start; use == false to check absence
CBecause strpos always returns boolean; use if(strpos()) to check presence
DBecause strpos returns length of substring; compare with 0 for absence
Step-by-Step Solution
Solution:
  1. Step 1: Understand return values of strpos

    strpos returns 0 if substring is at start, false if not found.
  2. Step 2: Correct way to check result

    Use strict comparison !== false to distinguish 0 (found) from false (not found).
  3. Final Answer:

    Because 0 is a valid position; check with strict !== false to avoid confusion -> Option A
  4. Quick Check:

    Use strict !== false to check strpos result [OK]
Quick Trick: Use strict !== false to check strpos result safely [OK]
Common Mistakes:
  • Using == false which treats 0 as false
  • Assuming strpos returns only boolean
  • Ignoring position 0 as valid result

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes