Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - File Handling
What will be the output of this PHP code?
$json = '{"name":"John","age":30}';
$obj = json_decode($json);
echo $obj->name;
Aname
BError: Cannot access property
CArray
DJohn
Step-by-Step Solution
Solution:
  1. Step 1: Decode JSON string to object

    json_decode without second argument returns an object with properties matching JSON keys.
  2. Step 2: Access object property

    Accessing $obj->name returns the string "John".
  3. Final Answer:

    John -> Option D
  4. Quick Check:

    Access object property after json_decode = value [OK]
Quick Trick: Access object properties with -> after json_decode [OK]
Common Mistakes:
  • Trying to access as array without true parameter
  • Expecting 'name' string instead of value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes