I use the error handling for my custom functions, and notice all three user error types (NOTICE, WARNING and ERROR) all return true.
I used the trigger_error function to escape a function when it has been incorrectly processed (eg. invalid data inserted). I thought the best way to escape the function is to use:
return trigger_error('Error notice here');
The problem is, errors return true. Although not all my functions return true upon success, it seems to me like this could be used in the future (hence, false would represent an error).
Is there a reason why this function would return true, or am I following bad practice in exiting invalid functions?
I know a solution for this could be:
trigger_error('Error notice here');
return false;
But I thought there would be a more elegant method. Hopefully there is, if not, some insight into best practices.
It returns
TRUEbecause the operation of triggering the error was successful. It would beFALSEif you specified an invalid error type to the second argument.If you want to return
TRUEand trigger an error (which, by the way, makes no sense the way I see it) in one line you can do this:Incidentally, if you trigger an
E_USER_ERRORI doubt the code that evaluates the return value would ever be reached, because the error would be fatal unless you have registered an error handler to catch it.If you want to exit a function with an error message that you can handle in your code, you should use an Exception. Although whether these make sense in the context of a procedural function is highly debatable.