Right now my pages look something like this:
if($_GET['something'] == 'somevalue')
{
$output .= 'somecode';
// make a DB query, fetch a row
//...
$row = $stmt->Fetch(PDO::ASSOC);
if($row != null)
{
$output .= 'morecode';
if(somethingIsOK())
{
$output .= 'yet more page output';
}
else
{
$error = 'something is most definitely not OK.';
}
}
else
{
$error = 'the row does not exist.';
}
}
else
{
$error = 'something is not a valid value';
}
if($error == '') // no error
{
//display $output on page
}
else // an error
{
// display whatever error occurred on the page
}
The way I'm doing things works, but it's very cumbersome and tedious for what is probably obvious: suppose that I call a function somewhere in the middle of my code, or want to check the value of a variable, or verify a DB query returned a valid result, and if it fails I want to output an error? I would have to make another if/else block and move all of the code inside the new if block. This doesn't seem like a smart way of doing things.
I have been reading about try/catch and have been thinking of putting all of my code inside a try statement, then let the code run sequentially without any if/else blocks and if something fails just throw an exception. From what I've read, that would halt the execution and make it jump straight to the catch block (just as a failed if statement will go to the else block), where I could then output the error message. But is that an acceptable or standard practice?
What's the best way of handling errors, fatal or not, in a php application that builds and outputs an HTML page? I don't want to just die with a blank screen, as that would be very user un-friendly, but instead want to output a message in the body of the page, still allowing the header and footer to show.
Thanks for your advice!
PDO error exception handling for queries, and really all code should be run through:
The reason for this, is it makes debugging much easier when you can pinpoint roughly where in lengthy scripts an error is occuring
more info here: http://php.net/manual/en/language.exceptions.php
If you're searching for a code structure which will look pretty and will work - you could use the
whitelist
method I always use. For example - validating a$_GET
variable:So, this is it , just 2
if/elseif
blocks, without nestingPHP has a built in class, ErrorException, for translating PHP errors into exceptions, which if unhandled, would naturally stop execution.
Exceptions have improved error handling mechanisms (try catch) and better debugging information (stack traces).
Include this at the top most of your execution path (the config, or something which is included first with all your code):
Although PDO supports throwing exceptions, it is off by default, you have to enable it:
If using MySQL, you also want an error for not setting mandatory fields and many other errors/warnings forgiven by default:
Exceptions can be handled like in many other programming languages using try catch finally:
When validating stuff, just throw exceptions:
throw new Exception("Missing URL variable userId!");
It would be nice if PHP made a clean break someday from the legacy error reporting thing and just throw exceptions by default (deprecate error_reporting() and change the default).
Using
try-catch
is one of the cleanest solutions you can use.I have made an example that still displays the header and footer when an error occurs, using your code converted to the
try-catch
format:PHP:
HTML:
References:
This is much more elegant and readable.
There are a lot of ways that you can deal with this and frankly none of them is intrinsically 'right'.
You will have to decide for yourself, which method is more 'comfortable' for you - it's always a mater of preferences (although there are certain techniques you should avoid and for good reasons).
It will highly depend on how you split your logic, however I tend to enclose all code that can return non-fatal errors inside a function, and use a return value of said function to indicate there was an error.
For fatal errors I tend to use exceptions (with
try-catch
blocks).Now just to be clear:
NTP
protocol, but the server didn't respond, you can decide to use localtime
function and still display a some valuable data to the user.SQL Exception
- there is no valuable data to be shown and you can only inform the user of this.Non-Fatal Errors (using function return)
A good example of using function-returns as a way of dealing with non-fatal problems would be a function that is trying to display content of some file on the page when this is not the main objective of the page (for example you would have a function that displays badges, fetched from a text file, on every single page - I know that this is far fetched but bear with me).
In fact, the function
fopen
itself is an example of this - it will return.Fatal-Errors (using exceptions - try-catch)
When you have some piece of code that needs to be executed because it's exactly what the user wanted (for example reading all news from database and displaying them to the user), you could use exceptions. Let's take a simple example - a user visited his profile and wanted to see all the messages he's got (let's assume, for now, that they are stored in plain text). You might have a function like:
And use it like this:
Now this could come in handy, if you had a 'top-level' script that would execute all the other code. That means that, for example, in your
index.php
you would just have:Do not overuse exceptions!
Whatever you do, never use exceptions as a way to check something you can recover from. Have a read on another question(full credit goes to Anton Gogolev for a very good explanation on this, as well as other answer-ers) as to why this is the case.
Further reading
Now there is no better way to learn how to deal with errors than to try several things and see what is good for you. You might find the below useful:
trigger_error()
function, which I haven't mentioned because I don't use it and don't know much about it, but apparently it's really useful. This is a particularly good read.Hope this helps :)