Bird
0
0

Which of the following is the correct syntax to compare the first 4 characters of two strings case-insensitively in PHP?

easy📝 Syntax Q3 of 15
PHP - String Functions
Which of the following is the correct syntax to compare the first 4 characters of two strings case-insensitively in PHP?
Astrncmp($str1, $str2, 4);
Bstrncasecmp($str1, $str2, 4);
Csubstr_compare($str1, $str2, 0, 4);
Dstrcasecmp($str1, $str2);
Step-by-Step Solution
Solution:
  1. Step 1: Understand function parameters

    strncasecmp() compares first n characters ignoring case; here n=4.
  2. Step 2: Check other options

    strncmp() is case-sensitive, substr_compare() is case-sensitive by default, strcasecmp() compares full strings.
  3. Final Answer:

    strncasecmp($str1, $str2, 4); -> Option B
  4. Quick Check:

    Case-insensitive first 4 chars = strncasecmp() [OK]
Quick Trick: Use strncasecmp() for case-insensitive partial string compare [OK]
Common Mistakes:
  • Using strncmp() which is case-sensitive
  • Using substr_compare() without case-insensitive flag
  • Using strcasecmp() without length limit

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes