If we know that a certain statement or block of code can throw an exception, we should wrap that code within the try…catch block to prevent the default error message being displayed and present a user-friendly error page. But before we proceed, let's create a function that will render an error message and exit the application. As we will be calling it from different script files, the best place for this function is, of course, the common.inc.php
file.
Our function, called showError()
, will do the following:
Render a heading saying "Error".
Render the error message. We will escape the text with the
htmlspecialchars()
function and process it with thenl2br()
function so that we can display multi-line messages. (This function will convert all line break characters to<br>
tags.)Call the
showFooter()
function to close the opening<html>
and<body>
tags. The function will assume that the application has already called theshowHeader()
function...