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();
}

No comments :

Post a Comment