What is the function json_decode() used for in PHP?

The json_decode() function in PHP is used to decode a JSON string and convert it into a PHP variable. This function is commonly used when working with APIs that return data in JSON format, allowing the data to be easily manipulated within a PHP script.

<?php

// JSON string to be decoded
$json_string = '{"name": "John", "age": 30, "city": "New York"}';

// Decode the JSON string into a PHP variable
$decoded_data = json_decode($json_string);

// Access the decoded data
echo $decoded_data->name; // Output: John
echo $decoded_data->age; // Output: 30
echo $decoded_data->city; // Output: New York

?>