5/01/2017

PHP7 Error Reporting & Error Catching: Not showing errors

In earlier versions of PHP it was easy to see your errors. For the most part, whenever there was an error, it would just show up on the screen. Not very secure, but helpful.

You could control the various types of errors that would appear using:
error_reporting(E_ALL & ~E_NOTICE) -- which meant: Show me all of the errors, but don't show the less important notices.

In later versions you had the ability to use try-catch blocks.

The basic usage looked like this:

try {
   // DO MY CODE HERE;
}
catch (EXCEPTION $e) {
  echo $e->getMessage();
}

As of PHP7,  this method has changed a little bit. Instead of EXCEPTION, we use THROWABLE in PHP7. To also support servers still using PHP5, you can add an additional clause to catch errors using the legacy code:

try {
   // DO MY CODE HERE;
}
catch (THROWABLE $t) {
   echo $t->getMessage();
}
catch (EXCEPTION $e) {
   echo $e->getMessage();
}

1 comment :

  1. Thank you so much for giving my family an update on this issue on your web-site. Please realise that if a brand new post appears or if perhaps any adjustments occur to the current post, I would be interested in reading a lot more and focusing on how to make good use of those strategies you reveal. Thanks for your efforts and consideration of other people by making this web site available. https://royalcbd.com/product/cbd-roll-on-gel/

    ReplyDelete