Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using <code>try-catch</code> is one of the cleanest solutions you can use.</p> <p>I have made an example that still displays the header and footer when an error occurs, using your code converted to the <code>try-catch</code> format:</p> <p><strong>PHP:</strong></p> <pre class="lang-php prettyprint-override"><code>&lt;?php try { $output = array(); if($_GET['something'] != 'somevalue') throw new Exception('something does not have a valid value.'); $output[] = 'Some Code'; $row = mt_rand(0, 10) &lt; 5 ? null : mt_rand(0, 100); if($row === null) throw new Exception('The row does not exist.'); $output[] = $row; if(!somethingIsOK()) throw new Exception('Something is most definitely not OK.'); $output[] = 'Yet more page output'; } catch(Exception $e) { $output[] = 'Error: ' . $e-&gt;getMessage(); // To show output and error $output = array('Error: ' . $e-&gt;getMessage()); // To only show error } function somethingIsOK() { return mt_rand(0, 10) &lt; 5; } ?&gt; </code></pre> <p><strong>HTML:</strong></p> <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE HTML&gt; &lt;html lang="en-US"&gt; &lt;head&gt; &lt;meta charset="UTF-8" /&gt; &lt;title&gt;PHP Error test&lt;/title&gt; &lt;style type="text/css"&gt; body { background: #eee; text-align: center } #content { padding: 60px } #header { padding: 30px; background: #fff } #footer { padding: 10px; background: #ddd } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="header"&gt;Header&lt;/div&gt; &lt;div id="content"&gt; &lt;?php echo implode('&lt;br /&gt;', $output); ?&gt; &lt;/div&gt; &lt;div id="footer"&gt;Footer&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>References:</strong></p> <ul> <li><strong><a href="http://php.net/manual/en/language.exceptions.php" rel="nofollow">PHP: Exceptions - Manual</a></strong></li> <li><strong><a href="http://php.net/manual/en/internals2.opcodes.catch.php" rel="nofollow">PHP: CATCH - Manual</a></strong></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload