How to handle the warning of file_get_contents() function in PHP?

Spread the love

Sometimes, we want to handle the warning of file_get_contents() function in PHP.

In this article, we’ll look at how to handle the warning of file_get_contents() function in PHP.

How to handle the warning of file_get_contents() function in PHP?

To handle the warning of file_get_contents() function in PHP, we can put @ before calling file_get_contents.

For instance, we write

if (($data = @file_get_contents("http://www.example.com")) === false) {
    $error = error_get_last();
    echo "HTTP request failed. Error was: " . $error['message'];
} else {
    echo "OK";
}

to call file_get_contents to get the content of http://www.example.com.

We use @ before calling it to suppress warnings.

And we check if it returns false.

If it does, then we get the error with error_get_last.

Otherwise, the request worked.

Conclusion

To handle the warning of file_get_contents() function in PHP, we can put @ before calling file_get_contents.

Leave a Reply

Your email address will not be published.