Posted By:
16 Mar, 2014 3:36 am PDT

Error Handling in PHP

How to handle errors in PHP?

We can have any kind of error in our code ,so we need to handle the error.

Example:the desired file you are looking for may not exit,so this error can be handled as:

 

Without Error handling:

<html>

<body>

<?php

$file=fopen("hello.txt","r");

?>

</body>                 

</html>

 

/*Output of the above program is:

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

No such file or directory in C:\webfolder\test.php on line 2

 */

With Error-Handling

<html>

<body>

<?php

if(!file_exists("hello.txt"))

  {

  die("File not found");

  }

else

  {

  $file=fopen("hello.txt","r");

  }

?>

</body>

</html>

 

 

/*Output of the above program is:

File not found  

*/

Attachment: error.php


Error may arise when programming code is prepared.

If errors are handled and checked properly ,your program looks professional.Thus error handling plays important role here.

Was it useful?  




Please login to reply to this problem.