How can the json_last_error function be helpful in debugging JSON-related issues in PHP?

The json_last_error function can be helpful in debugging JSON-related issues in PHP by providing information about the last JSON error that occurred. This can help identify issues such as syntax errors, encoding problems, or decoding failures. By checking the value returned by json_last_error, developers can quickly pinpoint the source of the problem and take appropriate action to resolve it.

$jsonString = '{"key": "value}';
$data = json_decode($jsonString);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'JSON Error: ' . json_last_error_msg();
    // Handle the JSON error here
} else {
    // Proceed with using the decoded JSON data
}