2009-09-17 86 views

回答

19

從您可以指定退出後()調用的函數,但在腳本結束之前,PHP幫助DOCO。

隨意檢查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'); 
?> 
6

如果你使用OOP,那麼你可以把你想要執行的代碼退出到你的類的析構函數中。

class example{ 
    function __destruct(){ 
     echo "Exiting"; 
    } 
} 
3

你的例子可能過於簡單,因爲它可以很容易地重新編寫如下:

if($result1 = task1()) { 
    $result2 = task2(); 
} 

common_code(); 
exit; 

也許你正在嘗試建立像這樣的流量控制:

do { 
    $result1 = task1() or break; 
    $result2 = task2() or break; 
    $result3 = task3() or break; 
    $result4 = task4() or break; 
    // etc 
} while(false); 
common_code(); 
exit; 

您也可以使用switch()

switch(false) { 
case $result1 = task1(): break; 
case $result2 = task2(): break; 
case $result3 = task3(): break; 
case $result4 = task4(): break; 
} 

common_code(); 
exit; 

或者PHP 5.3中,你可以使用goto

if(!$result1 = task1()) goto common; 
if(!$result2 = task2()) goto common; 
if(!$result3 = task3()) goto common; 
if(!$result4 = task4()) goto common; 

common: 
echo "common code\n"; 
exit; 
相關問題