PHP Custom Error Messages
Friday, June 08, 2007
This is a continuation to my post PHP Magic Constants.
Here I would like to demonstrate a real time example of magic constants.
I am going to write a user function for generating a user-level error/warning/notice message.
function myErrorFunction($error_message,$line,$file)
{
if($error_message!='')
{
$error_string = "User Error : {$error_message} in
{$file} on line number {$line}";
echo $error_string;
die();
}
}
myErrorFunction('test error message',__LINE__,__FILE__);
Now, you can call this function any where in your
script. It should print the error message as follows.
User Error : test error message in H:\test\ccodlib\test.php on line number 12
In the above function,
• die() is a portable way of causing the program to crash.
• __FILE__ is replaced (during compilation) with the name of the file containing the code (as a string).
• __LINE__ is replaced (during compilation) with the current line number (as an integer constant).
------------------
you can also achieve the above functionality by using the built in function “Trigger_error()” provided by PHP.
Labels : DiggIt! |
Del.icio.us