Bird
0
0

What is wrong with this PHP code?

medium📝 Debug Q7 of 15
PHP - File Handling
What is wrong with this PHP code?
$data = ['x' => 10, 'y' => 20];
$json = json_encode($data);
$arr = json_decode($json, false);
echo $arr['x'];
Aecho cannot print decoded JSON
Bjson_encode cannot encode associative arrays
Cjson_decode with false returns object, cannot use [] to access
Djson_decode second argument must be omitted for object
Step-by-Step Solution
Solution:
  1. Step 1: Understand json_decode second argument

    Passing false returns an object, not an array.
  2. Step 2: Accessing object properties

    Using [] on an object causes error; must use ->.
  3. Final Answer:

    json_decode with false returns object, cannot use [] to access -> Option C
  4. Quick Check:

    Use -> for object, [] for array after json_decode [OK]
Quick Trick: false or omitted returns object; true returns array [OK]
Common Mistakes:
  • Using [] on object
  • Confusing json_encode limitations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes