Bird
0
0

What is wrong with this PHP code?

medium📝 Debug Q7 of 15
PHP - Arrays
What is wrong with this PHP code?
$arr = ['a' => 'apple', 'b' => 'banana'];
$key = array_search('banana', $arr, true);
echo $key;
Aecho cannot output string keys
BThird parameter true causes strict type check error
Carray_search() does not accept a third parameter
DNo error; code works correctly
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_search() third parameter

    The third parameter enables strict type checking and is valid.
  2. Step 2: Check code correctness

    Searching 'banana' with strict check in array works fine; echo outputs key 'b'.
  3. Final Answer:

    No error; code works correctly -> Option D
  4. Quick Check:

    array_search() accepts third parameter [OK]
Quick Trick: Third parameter in array_search() enables strict check [OK]
Common Mistakes:
  • Thinking third parameter causes error
  • Believing echo can't print keys
  • Assuming array_search() only takes two parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes