Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[snippet] Custom PHP Error Pages

Hey, this is something I just found out which I've never actually seen explained anywhere or a tutorial on.

Actually my prof explained it, but I just found this too: http://uk3.php.net/manual/en/function.s ... andler.php

But it's really useful.

How to make custom PHP error pages

It's not very useful to your users if on you mistyping PHP and uploading it they get:

ERROR ERROR NEENAW NEENAW ERROR ON LINE 54

Or, as with some initial setups, just a white page.

So what can we do?

We create a function which will be called whenever an error is thrown, and then cancel out the initial error by returning "true" from the function.

How do I do it then!?

First we create out function which will replace the default one.

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
// code goes here
return true; // stops the default error function being used after
}


Now let's look at the parameters we're getting passed in.

$errno - a "code" for this error, tells us the basic type of error
$errstr - a string which tells us some information about the error
$errorfile - the file the error occurred in
$errline - the line of this file on which the error occurs

We can add some more important info here by using magic constants. If you aren't familiar with these we have for exaple:

__FILE__ - the file which is being processed. This is NOT the same as $errorfile, which could be an included file which is being used as part of this __FILE__.
__CLASS__ - class we're in
__DIR__ - directory of a file

So let's have a go at making an error function.

Sample Function

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
// code goes here
echo "An error has occurred!<br />";
echo "Error [" . $errno . "]: " . $errstr;
echo "<br />Occurred in: " . $errfile . " on line " . $errline . "called from file " . __FILE__;
return true;
}


Now we define this function as our new error handling method.


set_error_handler("myErrorHandler");
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top