Bird
0
0

Consider this PHP code to sort an array of strings ignoring case sensitivity. Which spaceship operator usage is correct inside the comparison function?

hard📝 Application Q9 of 15
PHP - Operators
Consider this PHP code to sort an array of strings ignoring case sensitivity. Which spaceship operator usage is correct inside the comparison function?
Ausort($arr, fn($a, $b) => strcasecmp($a, $b) <=> 0);
Busort($arr, fn($a, $b) => $a <=> $b);
Cusort($arr, fn($a, $b) => strtolower($a) <=> strtolower($b));
Dusort($arr, fn($a, $b) => strcmp($a, $b));
Step-by-Step Solution
Solution:
  1. Step 1: Understand case-insensitive comparison

    To ignore case, convert both strings to lowercase before comparing.
  2. Step 2: Identify correct spaceship usage

    usort($arr, fn($a, $b) => strtolower($a) <=> strtolower($b)); converts both strings to lowercase and compares with <=>, which is correct.
  3. Final Answer:

    usort($arr, fn($a, $b) => strtolower($a) <=> strtolower($b)); -> Option C
  4. Quick Check:

    Lowercase both strings before <=> for case-insensitive sort [OK]
Quick Trick: Convert strings to lowercase before <=> for case-insensitive sort [OK]
Common Mistakes:
  • Using strcasecmp with <=> incorrectly
  • Not handling case sensitivity
  • Using strcmp without case handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes