Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - Arrays
Identify the error in this PHP code snippet:
$arr = ['x' => 1, 'y' => 2];
$key = array_search(2);
echo $key;
Aarray_search() cannot search numeric values
BMissing second parameter (array) in array_search()
Cecho cannot print array keys
DArray keys must be integers
Step-by-Step Solution
Solution:
  1. Step 1: Check array_search() parameters

    array_search() requires two parameters: value and array.
  2. Step 2: Identify missing parameter

    Code calls array_search(2) without the array parameter, causing an error.
  3. Final Answer:

    Missing second parameter (array) in array_search() -> Option B
  4. Quick Check:

    array_search() needs value and array [OK]
Quick Trick: array_search() needs both value and array parameters [OK]
Common Mistakes:
  • Omitting array parameter
  • Thinking echo can't print keys
  • Believing keys must be integers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes