What is the purpose of json_decode in PHP and how is it used?
The purpose of json_decode in PHP is to decode a JSON string and convert it into a PHP variable. This function is commonly used when working with APIs or handling data sent in JSON format. It allows you to easily access and manipulate the data within the JSON string.
// JSON string to be decoded
$json_string = '{"name": "John", "age": 30, "city": "New York"}';
// Decode the JSON string into a PHP variable
$data = json_decode($json_string);
// Access the decoded data
echo $data->name; // Output: John
echo $data->age; // Output: 30
echo $data->city; // Output: New York