PHP函数超时处理方法

yipeiwu_com6年前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相关内容感兴趣的读者可查看本站专题:《PHP基本语法入门教程》、《PHP错误与异常处理方法总结》及《php常用函数与技巧总结

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP中的闭包(匿名函数)浅析

闭包也叫匿名函数 PHP5.3 引入。 使用方法 需要调整数组元素中的值 复制代码 代码如下: $data = range(0, 100);//想要每个元素的值都加上.html的后缀 $...

php计算整个目录大小的方法

本文实例讲述了php计算整个目录大小的方法。分享给大家供大家参考。具体实现方法如下: /** * Calculate the full size of a directory *...

PHP栈的定义、入栈出栈方法及基于堆栈实现的计算器完整实例

本文实例讲述了PHP栈的定义、入栈出栈方法及基于堆栈实现的计算器。分享给大家供大家参考,具体如下: 栈是线性表的一种,他的特点是后入先出,可以这么理解,栈就像一个存东西的盒子,先放进去的...

php常用的url处理函数总结

本文实例总结了几种在php中url编码解析函数,如parse_url、rawurldecode、rawurlencode、urldecode、urlencode.分享给大家供大家参考。具...

php计算到指定日期还有多少天的方法

本文实例讲述了php计算到指定日期还有多少天的方法。分享给大家供大家参考。具体如下: function countdays($d) { $olddate = substr($d,...