How can error reporting be used to troubleshoot issues with reading data from a URL in PHP?

When encountering issues with reading data from a URL in PHP, error reporting can be used to identify the specific problem. By enabling error reporting, PHP will provide detailed error messages that can help troubleshoot the issue. This can include errors such as incorrect URL format, network connectivity problems, or permission issues.

// Enable error reporting to display detailed error messages
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Example code to read data from a URL
$url = 'https://example.com/data.json';
$data = file_get_contents($url);

if ($data === false) {
    echo "Error reading data from URL";
} else {
    echo $data;
}