What is the purpose of using json_decode in PHP and how does it work?

The purpose of using json_decode in PHP is to convert a JSON string into a PHP variable. This is useful when working with APIs that return data in JSON format, as it allows you to easily manipulate and access the data within your PHP code.

// Sample JSON string
$jsonString = '{"name": "John", "age": 30, "city": "New York"}';

// Decode the JSON string into a PHP variable
$data = json_decode($jsonString);

// Access the data as a PHP object
echo $data->name; // Output: John
echo $data->age; // Output: 30
echo $data->city; // Output: New York