Error while connect to network services in PHP

Error when you connect to network service is another runtime error in PHP.
When you connect other machines using a network we can imagine how the other hardware fails. Or not just the hardware, there are a lot of devices and services that are not under your control. That is why you really need to carefully check the return value of functions that attempt to interact with a network service.

Let me give one example – if we call a function like that:

It will not return an error if the attempt for connection to port 443 on the domain failed. It will provide a warning, will display it in the default format, and not give your many options to handle it easily.

But if we write the code like that:

Will check the return value to see what connection error occurred an error occurred, and you can use your own code to handle the error message. As the code is written, it will display an error message that might help you solve the problem. In this case, it would produce the following output:

Handling runtime errors requires a careful consideration to check for different types of failure that might occur and then take appropriate action. MySQL can provide one of around 200+ different error numbers and messages and a big part of them are connection error messages.You do need to simulate an error in each function call that is likely to result in an error and an error of each type that is handled by a different block of code.

Filed Under: PHP

Anthony Gee About the Author: Anthony G. is an IT specialist with more than 9 years of solid working experience in the Web Hosting industry. Currently works as server support administrator, involved in consultative discussions about Web Hosting and server administration. One of the first writers in the website, now writing for Free Tutorials community - he is publishing tutorials and articles for the wide public, as well as specific technical solutions.

Leave a Reply