Oct
30
Posted on 30-10-2007
Filed Under (Error Solving) by chintan

The error handling in PHP is very easy. An error message with filename, line number and a message describing the error are sent to the browser.

When generate scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unethical and you may be open to security risks.

Using the die() function

The first illustration shows a simple script that opens a text file:

<?php

$file=fopen(“chi1.txt”,”c”);

?>

If the file does not exist you might get an error like this:

Advice: fopen(chi1.txt) [function.fopen]: failed to open stream:

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

To avoid that the user gets an error message like the one above, we test if the file exist before we try to access it:

<?php

if(!file_exists(“chi1.txt”))

{

die(“File not found”);

}

else

{

$file=fopen(“chi1.txt”,”c”);

}

?>

Now if the file does not exist you get an error like this:

File not found

The code above is more competent than the earlier code, because it uses a simple error handling device to stop the script after the error.

Share These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Live
  • StumbleUpon
  • Technorati
  • YahooMyWeb
(0) Comments    Read More   
Post a Comment

You must be logged in to post a comment.