How can one ensure proper error handling and debugging when encountering issues with file_get_contents in PHP?

When encountering issues with file_get_contents in PHP, it is important to ensure proper error handling and debugging to identify the root cause of the problem. One way to do this is by using the file_get_contents function within a try-catch block to catch any exceptions that may occur. Additionally, you can use the error_reporting function to display any errors or warnings that may be generated during the execution of the script.

<?php

$url = 'https://www.example.com';

try {
    $content = file_get_contents($url);
    if ($content === false) {
        throw new Exception('Failed to retrieve content from URL');
    }
    echo $content;
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

error_reporting(E_ALL);