Bird
0
0

How can you compare two strings in PHP to check if they are equal ignoring case and whitespace differences at the start and end?

hard📝 Application Q9 of 15
PHP - String Functions
How can you compare two strings in PHP to check if they are equal ignoring case and whitespace differences at the start and end?
AUse strcasecmp(trim($str1), trim($str2))
BUse strcmp(trim($str1), trim($str2))
CUse strncasecmp($str1, $str2, strlen($str1))
DUse substr_compare($str1, $str2, 0, strlen($str1))
Step-by-Step Solution
Solution:
  1. Step 1: Remove whitespace from both strings

    Use trim() to remove spaces at start and end.
  2. Step 2: Compare ignoring case

    Use strcasecmp() on trimmed strings to ignore case differences.
  3. Final Answer:

    Use strcasecmp(trim($str1), trim($str2)) -> Option A
  4. Quick Check:

    Trim then case-insensitive compare = strcasecmp(trim()) [OK]
Quick Trick: Trim strings before strcasecmp() to ignore whitespace [OK]
Common Mistakes:
  • Using strcmp() which is case-sensitive
  • Not trimming strings before compare
  • Using strncasecmp() without trimming

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes