I can't do something like this ?
try { require_once( '/includes/functions.php' ); } catch(Exception $e) { echo "Message : " . $e->getMessage(); echo "Code : " . $e->getCode(); }
No error is echoed, server returns 500.
The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.
include_once will throw a warning, but will not stop PHP from executing the rest of the script. require_once will throw an error and will stop PHP from executing the rest of the script.
The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.
The basic difference between require and require_once is require_once will check whether the file is already included or not if it is already included then it won't include the file whereas the require function will include the file irrespective of whether file is already included or not.
You can do it with include_once
or file_exists
:
try { if (! @include_once( '/includes/functions.php' )) // @ - to suppress warnings, // you can also use error_reporting function for the same purpose which may be a better option throw new Exception ('functions.php does not exist'); // or if (!file_exists('/includes/functions.php' )) throw new Exception ('functions.php does not exist'); else require_once('/includes/functions.php' ); } catch(Exception $e) { echo "Message : " . $e->getMessage(); echo "Code : " . $e->getCode(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With