Any way to change the formatting of the text and what is displayed after a die() command? I'm currently coding all my errors using die() but I'd like to have more control over how the error message is formatted and what is displayed afterwards (like a return link for example)

Dani AI

Generated

Building on ’s idea of centralizing messages and ’s pointer to custom handlers, a better path is not to try to “alter” die() but to centralize termination and rendering in one place. Register an error/exception pipeline that formats output, logs details, sets an HTTP status, and provides a consistent “return” link or UI. That makes formatting, localization, and logging uniform across the app and avoids sprinkling raw die() messages in many files.

Practical skeleton (register handlers, convert notices to exceptions, catch fatal shutdowns, and render a single HTML error page):

<?php
function render_error_page($title, $message, $status = 500) {
    if (!headers_sent()) {
        http_response_code($status);
        header('Content-Type: text/html; charset=utf-8');
    }
    $safe = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
    echo "<!doctype html><html><head><meta charset='utf-8'><title>{$title}</title></head><body>";
    echo "<h1>{$title}</h1><p>{$safe}</p>";
    echo "<p><a href=\"" . ($_SERVER['HTTP_REFERER'] ?? '/') . "\">Return</a></p>";
    echo "</body></html>";
    exit;
}

set_error_handler(function($errno, $errstr, $errfile, $errline) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

set_exception_handler(function($ex) {
    error_log($ex);
    render_error_page('Application error', 'An unexpected error occurred. Please try again later.', 500);
});

register_shutdown_function(function() {
    $err = error_get_last();
    if ($err && ($err['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR))) {
        error_log(print_r($err, true));
        render_error_page('Fatal error', 'A fatal error occurred.', 500);
    }
});
?>

Notes and cautions: do not display stack traces or sensitive paths in production—log them with error_log and show a generic message. Use http_response_code to return appropriate status codes. If you have many existing die() calls, replace them gradually with a small wrapper that calls your renderer or throw exceptions instead. For reference implementation details see PHP’s manual pages for set_error_handler, set_exception_handler and register_shutdown_function/error_get_last.

Recommended Answers

All 3 Replies

Any way to change the formatting of the text and what is displayed after a die() command? I'm currently coding all my errors using die() but I'd like to have more control over how the error message is formatted and what is displayed afterwards (like a return link for example)

One thing to bear in mind here is that die() is a language construct, not a function, but I think I see where you are going with this.

You could have a preset list of definitions for errors in the beginning of your script, then use them for your output. For example:

<?php

define("ERROR1", "Message for error number 1.");
define("ERROR2","A message with <a href='javascript:history.go(-1)'>a return to the previous page link</a> embedded.");

...

$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
   or exit("unable to open file ($filename): ".ERROR2);

?>

This sounds like what you were looking for, but if I misunderstood, please feel free to clarify.

My 2¢. :)

One thing to bear in mind here is that die() is a language construct, not a function, but I think I see where you are going with this.

You could have a preset list of definitions for errors in the beginning of your script, then use them for your output. For example:

<?php

define("ERROR1", "Message for error number 1.");
define("ERROR2","A message with <a href='javascript:history.go(-1)'>a return to the previous page link</a> embedded.");

...

$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
   or exit("unable to open file ($filename): ".ERROR2);

?>

This sounds like what you were looking for, but if I misunderstood, please feel free to clarify.

My 2¢. :)

Your 2 cents are work 2 billion :p Thanks a lot that is what i meant

die() is a fairly simplistic command. It puts a halt to script execution and echos the string in the argument.
I would recommend that you look into creating some custom error handlers at http://w3schools.com/php/php_error.asp.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.