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