Search code examples
phphtmlformattingmessagedie

Format die(); Message in PHP


Is there a way to style the output of php's die(); message?

I want to add some HTML and CSS around the error message so that I could use it in a productive environment.


Solution

  • If you're using the actual die() function, it will always print out exactly what text you pass it (which could be HTML, but might be awkward to use).

    You could, however, simply make your own function that prints out an error message nicely and then calls exit - that way you don't have to repeat the HTML that you might otherwise pass to die() every time.

    function die_nicely($msg) {
        echo <<<END
    <div id="critical_error">$msg</div>
    END;
        exit;
    }