Handling Error in PHP Coding

Asked By 30 points N/A Posted on -



Is there anyone can help on how to handle Error in PHP? Instead of displaying this error below:

Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:

No such file or directory in C:webfoldertest.php on line 2

I want it to display a sentence that there is an error for example “Error found” or “File not Found” instead of the Error displayed above.


Answered By 5 points N/A #106189

Handling Error in PHP Coding


Dear friend

  • Error handling is considered as an important component while creation of scripts and web applications. Any code developed which does not have or lacks error checking code is considered very unprofessional. Worse it very much opens gates to security risks.
  • With respect to your issue, you should handle the error failure for open stream with proper message  “File Not Found” as provided below.

In order to avoid such error, first see if the file exists or not as depicted in the code below:

  die("File not found");
//used to display user exception message

This code is known as basic error handling with die function.

One is also allowed for setting custom error handler, handling error by try, catch and finally. In order to understand more on   exception handling please read PHP tutorial.

Login/Register to Answer

Related Questions