views:

387

answers:

4
+1  Q: 

Fatal error php

Is there a way to make the code continue (not exit) when you get a fatal error in PHP? For example I get a timeout fatal error and I want whenever it happens to skip this task and the continue with others. In this case the script exits.

+5  A: 

There is a hack using output buffering that will let you log certain fatal errors, but there's no way to continue a script after a fatal error occurs - that's what makes it fatal!

If your script is timing out you can use set_time_limit() to give it more time to execute.

Greg
+1  A: 

It depends on the exact error type. You can catch errors by creating your own error handler. See the documentation on set_error_handler(), but not all types of errors can be caught. Look at the timeout error you get and see what type it is. If it is one of E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR or E_COMPILE_WARNING then you cannot catch it with an error handler. If it another type then you can. Catch it with the error handler and simply return.

Sander Marechal
A: 

Is there a way then to limit the execution time of an function but not all script? For example function blabla() { return "yes"; }

to make it so that if it is not executed in 25 seconds to return no;

Granit
no, you cant limit a function's execution time.
Quamis
+2  A: 

"Fatal Error", as it's name indicates, is Fatal : it stop the execution of the script / program.

If you are using PHP to generate web pages and get a Fatal error related to max_execution_time which, by defaults, equals 30 seconds, you are certainly doing something that really takes too mych time : users won't probably wait for so long to get the page.

If you are using PHP to do some heavy calculations, not in a webpage (but via CLI, or a cron, or stuff like that), you can set another (greater) value for max_execution_time. You have two ways of doing that :

First is to modify php.ini, to set this value (it's already in the file ; just edit the property's value). Problem is it'll modify it also for the web server, which is bad (this is a security measure, after all). Better way is to create a copy of php.ini, called, for instance, phpcli.ini, and modify this file. Then, use it when invoking php :

php -c phpcli.ini myscript.php

This'll work great if you have many properties you need to configure for CLI execution. (Like memory_limit, which often has to be set to a higher value for long-running batches)

The other way is to define a different value for max_execution_time when you invoke php, like this :

php -d max_execution_time=60 myscript.php

This is great if you launch this via the crontab, for instance.

Pascal MARTIN