Why would PHP include_once succeed but require_once, on the same file, fail?

364 Views Asked by At

I have a PHP script that uses require_once to load modules. It's run hourly by CRON and was working OK Since adding another module, it still works OK overnight (presumable when the load is low) but frequently fails (crashes) during the day. I added some monitoring code to the functions in the module to see whee it fails and it's definitely where it tries to load the module. In an attempt to ascertain why the script was failing to load the module, I changed the require_once to include_once. For some reason, that seems to have solved the problem - the functions in the module are being executed. Can anyone explain why require_once fails to load the module but include_once succeeds? Has anyone even encountered this problem before?

1

There are 1 best solutions below

3
On

The require and include functions do the same task, the small difference is :

  • require will produce a fatal (E_COMPILE_ERROR) level error when the specified file location is invalid or for any error
  • include will generate a warning (E_WARNING) and continue the code execution.

See : Click require and include

So you did not solve the problem, you've just transform the error level from ERROR to WARNING, that's why your script is working.