PHP中函数超时如何处理

今天就跟大家聊聊有关PHP中函数超时如何处理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

register_shutdown_function

Registers the function named by function to be executed when script processing is complete or when exit() is called.

此函数可以重复注册,然后会依次调用 当发生致命性错误或者exit时都会调用此函数

error_reporting(0);
register_shutdown_function ( 'handleShutdown' );
function handleShutdown (){
  $error = error_get_last ();
  // 根据错误信息,判断是否是超时了
  if ( $error && strpos ( $error [ 'message' ], 'Maximum exec' )!== false )
  {
    echo 'handle time out';
  }
}
set_time_limit(2);
sleep(3);

看完上述内容,你们对PHP中函数超时如何处理有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。