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);
}
?>
