What role does the json_decode() function play in handling API responses in PHP?
When working with API responses in PHP, the json_decode() function is used to convert a JSON string into a PHP variable. This allows you to easily access and manipulate the data returned by the API.
// Sample API response
$response = '{"name": "John", "age": 30}';
// Decode the JSON response
$data = json_decode($response);
// Access the data
echo $data->name; // Output: John
echo $data->age; // Output: 30
Related Questions
- What are the implications of not displaying SQL errors when executing queries in PHP, and how can this be improved for troubleshooting purposes?
- What is the best approach to drawing a rotated rectangle using GD in PHP?
- What are the risks associated with using object names for session storage in PHP?