IF require_once ELSE

Is there a way to use IF ELSE on REQUIRE_ONCE? I’d like to have an alternative occur if the required file fails to load.

Yes and No…

First, Require and Require_Once are basically “Include”'s. So, it assumes you are including a file you wrote and does no checking for the file. If missing, it will just NOT load the file.

So, a simple way to work around this issue would be to check to see if the file exists first.
If it does, then do the Require_Once. If not, do whatever you want if it is missing…
Here is sample code to do this:
[php]

<?php $filename = '/path/to/IncludeFile.PHP'; if (file_exists($filename)) { Require_Once($filename); } else { echo "The file 'IncludeFile.PHP' does not exist"; } ?>

[/php]
Not tested, just threw it to

I feel blond actually, the solution is nothing more than this…
[php]
if (!require_once ‘’) {} else {}
[/php]

Depends on the include type, require_once and require will throw out error messages if it can’t be loadedl. include and include_once just wont’ load the file, but it typically won’t give out an error message (depends on how error reporting is configured on your host.)

Sponsor our Newsletter | Privacy Policy | Terms of Service