What potential issues can arise when parsing JSON data in PHP, and how can they be addressed?

One potential issue when parsing JSON data in PHP is that the data may contain unexpected characters or formatting errors, leading to parsing errors. This can be addressed by using error handling techniques, such as try-catch blocks, to handle parsing errors gracefully and prevent the script from crashing.

// Example code snippet demonstrating error handling when parsing JSON data
$jsonData = '{"name": "John", "age": 30, "city": "New York"}';

try {
    $parsedData = json_decode($jsonData);
    
    if ($parsedData === null && json_last_error() !== JSON_ERROR_NONE) {
        throw new Exception('Error parsing JSON data: ' . json_last_error_msg());
    }

    // Process the parsed data here
    var_dump($parsedData);
} catch (Exception $e) {
    echo 'An error occurred: ' . $e->getMessage();
}