Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to handle errors on a php page?
    primarykey
    data
    text
    <p>Right now my pages look something like this:</p> <pre><code>if($_GET['something'] == 'somevalue') { $output .= 'somecode'; // make a DB query, fetch a row //... $row = $stmt-&gt;Fetch(PDO::ASSOC); if($row != null) { $output .= 'morecode'; if(somethingIsOK()) { $output .= 'yet more page output'; } else { $error = 'something is most definitely not OK.'; } } else { $error = 'the row does not exist.'; } } else { $error = 'something is not a valid value'; } if($error == '') // no error { //display $output on page } else // an error { // display whatever error occurred on the page } </code></pre> <p>The way I'm doing things works, but it's very cumbersome and tedious for what is probably obvious: suppose that I call a function somewhere in the middle of my code, or want to check the value of a variable, or verify a DB query returned a valid result, and if it fails I want to output an error? I would have to make another if/else block and move all of the code inside the new if block. This doesn't seem like a smart way of doing things.</p> <p>I have been reading about try/catch and have been thinking of putting all of my code inside a try statement, then let the code run sequentially without any if/else blocks and if something fails just throw an exception. From what I've read, that would halt the execution and make it jump straight to the catch block (just as a failed if statement will go to the else block), where I could then output the error message. But is that an acceptable or standard practice?</p> <p>What's the best way of handling errors, fatal or not, in a php application that builds and outputs an HTML page? I don't want to just die with a blank screen, as that would be very user un-friendly, but instead want to output a message in the body of the page, still allowing the header and footer to show.</p> <p>Thanks for your advice! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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