php – register_shutdown_function做什么?

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函数.你在开始注册了,现在你知道如果你退出或你的脚本完成,那么你的功能被调用.

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

作者: dawei

【声明】:滨州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部