I have this big function (1300+ lines of code) that takes data from the web and insert it into a local database. Each time the function runs its takes something like 20 seconds to complete and I need to run this function like a million times, so I use set_time_limit(0) to set the PHP time limit to infinite and I loop the function a million times, like this:
for ($ID= '01'; $ID < '999999'; $ID++) {
getDataFromWeb($conn, $ID);
}
So whats the problem? The problem is that there are a million things that can go wrong and it always does go wrong, and suddenly the code gets stuck in ID 23465 for example, and it just stop getting data but I don't get any kind of error, its like the loop continues but without inserting anything to database, and because of the 'no time limit' I set to PHP then it never stops.
I want to know how I can detect this kind of problem, stop all and show alert. If a I set the time before the function starts and then check it when the function ends, like this:
for ($ID= '01'; $ID < '999999'; $ID++) {
$time_start = microtime();
getDataFromWeb($conn, $ID);
$time_end = microtime();
if ($time_alert - //... somehow check how time does it takes and stop if its taking too much
}
It will not work because if the function never completes then $time_end will never be set and so on...
So, help please?