📖 Scenario: You are working on a simple PHP script that needs to handle user data. You will practice converting PHP arrays to JSON format and back, which is common when sending or receiving data in web applications.
🎯 Goal: Build a PHP script that creates an array of user information, converts it to a JSON string, then decodes the JSON back to a PHP array, and finally prints the decoded data.
📋 What You'll Learn
Create a PHP array with specific user data
Create a variable to hold the JSON encoded string
Decode the JSON string back to a PHP array
Print the decoded array in a readable format
💡 Why This Matters
🌍 Real World
Web applications often send and receive data in JSON format between the server and client. Knowing how to encode and decode JSON in PHP is essential for handling API responses and requests.
💼 Career
Many PHP developer roles require working with JSON data for REST APIs, AJAX calls, and configuration files. This skill is fundamental for backend and full-stack developers.
Progress0 / 4 steps
1
Create the user data array
Create a PHP array called user with these exact key-value pairs: 'name' => 'Alice', 'age' => 30, and 'city' => 'New York'.
PHP
Hint
Use square brackets to create the array and assign the keys and values exactly as shown.
2
Encode the array to JSON
Create a variable called jsonString and set it to the JSON encoded string of the user array using json_encode().
PHP
Hint
Use the json_encode() function and assign the result to $jsonString.
3
Decode the JSON string back to an array
Create a variable called decodedUser and set it to the result of decoding jsonString back to a PHP array using json_decode() with the second parameter set to true.
PHP
Hint
Use json_decode() with the second argument true to get an associative array.
4
Print the decoded array
Use print_r() to print the decodedUser array so it is readable.
PHP
Hint
Use print_r($decodedUser); to display the array in a readable format.
Practice
(1/5)
1. What does the PHP function json_encode() do?
easy
A. Converts PHP data into a JSON formatted string
B. Converts JSON string into PHP data
C. Validates JSON syntax
D. Reads JSON data from a file
Solution
Step 1: Understand the purpose of json_encode()
This function takes PHP variables like arrays or objects and converts them into a JSON string format.
Step 2: Differentiate from json_decode()
json_decode() does the opposite by converting JSON strings back to PHP data.
Final Answer:
Converts PHP data into a JSON formatted string -> Option A
Quick Check:
json_encode() = PHP to JSON string [OK]
Hint: json_encode() makes PHP data into JSON text [OK]
Common Mistakes:
Confusing json_encode() with json_decode()
Thinking json_encode() reads JSON from files
Assuming json_encode() validates JSON syntax
2. Which of the following is the correct syntax to decode a JSON string into a PHP array?
easy
A. $data = json_encode($jsonString, true);
B. $data = json_decode($jsonString, true);
C. $data = json_decode($jsonString);
D. $data = json_encode($jsonString);
Solution
Step 1: Identify the function to decode JSON
The function to convert JSON string to PHP data is json_decode().
Step 2: Use the second parameter to get an array
Passing true as the second argument returns an associative array instead of an object.
Final Answer:
$data = json_decode($jsonString, true); -> Option B
Quick Check:
json_decode() with true returns array [OK]
Hint: Use json_decode($json, true) for PHP arrays [OK]
Common Mistakes:
Using json_encode() instead of json_decode()
Omitting the second parameter to get an array
Passing true to json_encode() which is invalid
3. What will be the output of the following PHP code?