"best way to handle errors on a php page?" Code Answer


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:

  • a non-fatal error is an error that you can recover from - meaning that even though something went wrong, there is still some code that can be executed and generate some valuable output. for example if you wanted to get current time using ntp protocol, but the server didn't respond, you can decide to use local time function and still display a some valuable data to the user.
  • a fatal error is an error that you would not be able to recover from - meaning that something really bad happened and the only thing you can do is tell your user that page cannot do what it was asked to. for example if you were fetching some data from your database and got 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).

function getbadge($file){
    $f = fopen($file,'r');
        return null;
    .. do some processing ..
    return $badges;

$badges = getbadges('badges.txt');
    echo "cannot display badges.";
} else {
    echo $badges;
.. carry on doing whatever page should be doing ..

in fact, the function fopen itself is an example of this - it will return.

returns a file pointer resource on success, or false on error.

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:

function getmessages($user){
    $messages = array();
    $f = fopen("messages_$user.txt","r");
        throw new exception("could not read messages!");
    ... do some processing ...
    return $messages;

and use it like this:

    ..do some stuff..
    $messages = getmessages($_session['user'])); //assuming you store username in $_session
    foreach($messages as $msg){
        echo $msg."<br/>";
} catch(exception $e){
    echo "sorry, there was an error: ".$e->getmessage();

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:

    .. execute some code, perform some functions ..
} catch(exception $e){
    echo "sorry, there was an error: ".$e->getmessage();

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:

  • w3school on php exception handling
  • short tutorial on error handling(similar to my function-returns method)
  • extensive tutorial on php error handling - including using 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 :)

By Danish Khan on August 18 2022

Answers related to “best way to handle errors on a php page?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.