PHP脚本可以在exit()之前执行公共代码吗?

Rob*_*cks 6 php scripting webserver

如何在PHP脚本中完成以下内容?

 code{
      $result1 = task1() or break;
      $result2 = task2() or break;
 }

 common_code();
 exit();
Run Code Online (Sandbox Code Playgroud)

Jai*_*Jai 20

从PHP帮助doco中,您可以指定在exit()之后但在脚本结束之前调用的函数.

请随时查看doco以获取更多信息http://us3.php.net/manual/en/function.register-shutdown-function.php

<?php
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');
?>
Run Code Online (Sandbox Code Playgroud)


Mik*_*key 6

如果你使用OOP,那么你可以将你想要执行的代码放在你的类的析构函数中.

class example{
   function __destruct(){
      echo "Exiting";
   }
}
Run Code Online (Sandbox Code Playgroud)