加入收藏 | 设为首页 | 会员中心 | 我要投稿 莱芜站长网 (https://www.0634zz.com/)- 云连接、建站、智能边缘云、设备管理、大数据!
当前位置: 首页 > 编程开发 > PHP > 正文

php – register_shutdown_function做什么?

发布时间:2020-07-16 01:20:23 所属栏目:PHP 来源:互联网
导读:register_shutdown_function做什么 这实际上是一个非常简单的功能.所以你有一个php脚本: ?php echo Here is my little script; function endScript(){ echo My little Script has finished; } register_shutdown_functi

register_shutdown_function做什么 这实际上是一个非常简单的功能.所以你有一个php脚本:

<?php
  echo "Here is my little script";

  function endScript(){
       echo "My little Script has finished";
  }

  register_shutdown_function('endScript');

?>

基本上,脚本运行和完成后会发生什么,因为我们添加了这一行register_shutdown_function(‘endScript’);它会调用函数endScript,最后输出我的小脚本已经完成了.

这里有一点点从documentation:

Registers a callback to be executed after script execution finishes or exit() is called.Multiple calls to register_shutdown_function() can be made,and each will be called in the same order as they were registered. If you call exit() within one registered shutdown function,processing will stop completely and no other registered shutdown functions will be called.

*更新

当您经常在php中使用名为exit()的函数时,很有用.

例如:

<?php 

  function endScript(){
       echo "My little Script has finished";
  }
  register_shutdown_function('endScript');

  $result = some_intricate_function()

  if($result == 0){
     exit();
  } 

  $result = some_other_intricate_function()

  if($result == 0){
     exit();
  }

/* and this keeps reoccurring */
?>

现在注意到,即使您多次呼叫退出,您也不必在退出之前调用endScript函数.你在开始注册了,现在你知道如果你退出或你的脚本完成,那么你的功能被调用.

现在我的关机功能当然是没用的;但是如果您需要清理的东西(例如,关闭打开的文件句柄,保存一些持久数据等),它将变得有用

(编辑:莱芜站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读