Php 设置回调的时间限制?

Php 设置回调的时间限制?,php,Php,有没有可能设置一个“回调函数”来执行某些操作,而不是在达到时间限制时返回错误?我不这么认为……这种做法有违函数的初衷。。。我想如果你想做这样的事情,把它设为0(没有时间限制),用不同的方式记录时间,然后在X时间之后做一些事情 您可以删除时间限制(设置时间限制(0)),然后跟踪PHP脚本开始执行的时间,并与当前时间进行比较 <?php $startTime = microtime(); // Whatever you need to do... // Place this where

有没有可能设置一个“回调函数”来执行某些操作,而不是在达到时间限制时返回错误?

我不这么认为……这种做法有违函数的初衷。。。我想如果你想做这样的事情,把它设为0(没有时间限制),用不同的方式记录时间,然后在X时间之后做一些事情

您可以删除时间限制(
设置时间限制(0)
),然后跟踪PHP脚本开始执行的时间,并与当前时间进行比较

<?php

$startTime = microtime();

// Whatever you need to do...

// Place this where it needs to be in the context of your application.
if (microtime() - $startTime > 15000) { 
    // Time's up!
}
正如其他人所说的那样。
如果您的脚本由于外部因素而超时(比如它没有从单个函数调用返回),那么您唯一的补救方法可能是使用和。尽管我不确定这些是否可以用于sapi的other then CLI。

您可以尝试以下方法:


请不要在标题中写标签。@Tomalak我永远都在编辑它们。@alex:我也是。情况似乎越来越糟:(
function shutdown()
{
    // This is our shutdown function, in 
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;
}

register_shutdown_function('shutdown');