Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - File Handling
Identify the error in this PHP code snippet:
$json = '{"fruit":"apple","count":3}';
$data = json_decode($json, true);
echo $data->fruit;
Ajson_decode cannot parse this JSON
BIncorrect JSON string format
CMissing second parameter in json_decode
DUsing object syntax on an array after json_decode
Step-by-Step Solution
Solution:
  1. Step 1: Check json_decode parameters

    The second parameter true makes json_decode return an associative array.
  2. Step 2: Accessing array with object syntax

    Using $data->fruit tries to access an object property, but $data is an array, so this causes an error.
  3. Final Answer:

    Using object syntax on an array after json_decode -> Option D
  4. Quick Check:

    json_decode(true) returns array, use $data['fruit'] [OK]
Quick Trick: Use $data['key'] if json_decode(true) used [OK]
Common Mistakes:
  • Accessing array as object after json_decode(true)
  • Assuming JSON string is invalid
  • Forgetting to pass true for array output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes