Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - File Handling
Identify the error in this PHP code snippet:
$json = '{"a":1, "b":2}';
$arr = json_decode($json);
echo $arr['a'];
Aecho cannot print array elements
BJSON string is invalid
Cjson_decode requires second argument true to work
Djson_decode returns object, not array, so use -> instead of []
Step-by-Step Solution
Solution:
  1. Step 1: Check json_decode default return type

    json_decode returns an object by default, not an array.
  2. Step 2: Accessing object properties

    Accessing with array syntax [] causes error; use -> for objects.
  3. Final Answer:

    json_decode returns object, not array, so use -> instead of [] -> Option D
  4. Quick Check:

    Access object with ->, array with [] [OK]
Quick Trick: Use -> for objects, [] for arrays after json_decode [OK]
Common Mistakes:
  • Using [] on object returned by json_decode
  • Assuming json_decode always returns array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes