Error Handling with PHP

Error Handling with PHP

As with any programming language, when you code in PHP, it helps immensely if you set up your applications to handle errors gracefully. PHP has little advanced support for handling errors.

Getting started with PHP is quite easy. Moreover, given a little more time, an average developer can pick up a good understanding of object-oriented programming and start working with classes and objects. Definitely, this sounds like a good and exciting thing — except for when, during the development of an application, one has to write error handling code.

One of the simple and basic function to handle is die()

<?php

mysql_connect(’localhost’,'user’,'pass’) or die(’Could not connect to database’);

?>

The “die()� statement can be used to build in simple error handling mechanisms. But, particularly in mid-sized and large applications, this method isn’t flexible enough. As I said before, PHP provides developers with the “trigger_error()� function, which can be utilized for developing a slightly more sophisticated error handler.

<?php

if ($divisor == 0) {
trigger_error(”Cannot divide by zero”, E_USER_ERROR);
}
?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.