2012-07-07 140 views
0

我試圖第一次設置錯誤處理。
它確實工作並報告錯誤(如果有),但出於某種原因,它總是顯示錯誤處理函數本身的「缺少參數」的錯誤。請注意我的錯誤處理功能是在一個單獨的文件中,包括索引頁,我不知道如果是這樣的問題:SPHP錯誤處理 - 「errorHandler缺少參數」

這裏是我的錯誤處理功能

function errorHandler($errno, $errstr, $error_file, $error_line) { 

    if(isset($errstr)) { 
    # There is an error, display it 
    echo $errno." - ".$errstr." in ".$error_file." at line ".$error_line."<br>"; 
    } else { 
    # There isn't any error, do nothing 
    return false; 
    } 

} 

// We must tell PHP to use the above error handler. 
set_error_handler("errorHanlder"); 

這裏是索引頁

<!-- # Error Handler --> 
<? if(errorHandler()) { ?> 
<section id="error-handler"> 
    <?=errorHandler();?> 
</section> 
<? } ?> 

下面是結果在瀏覽器(記住,沒有PHP的錯誤,所以這個錯誤處理程序不應該輸出任何東西 - 這是我不明白

2 - Missing argument 1 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10 
2 - Missing argument 2 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10 
2 - Missing argument 3 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10 
2 - Missing argument 4 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10 

任何想法的原因是爲什麼PHP報告缺少的參數?

回答

7

要調用零參數的函數...

<?=errorHandler();?> 

爲什麼你總有需要打電話了嗎?

並且在您的代碼中有幾個拼寫錯誤:將「Hanlder」替換爲「Handler」。

有沒有需要做的是:當有錯誤

if(isset($errstr)) { 

你的錯誤處理功能被自動調用(且只有在這種情況下!)。 $ errstr是這個函數的一個參數,它在函數執行時總是被設置。

新代碼:

function errorHandler($errno, $errstr, $error_file, $error_line) { 
    # There is an error, display it 
    echo "<section id='error-handler'>$errno - $errstr in $error_file at line $error_line</section><br>"; 
} 

// We must tell PHP to use the above error handler. 
set_error_handler("errorHandler"); 
+0

我想了解這一切的作品。 如果我將$ errno,$ errstr,$ errfile,$ errline的參數傳遞給errorHandler,那麼它會輸出相同的結果。修正了錯別字 – Clarkey 2012-07-07 12:00:28

+0

Jocelyn,謝謝你的回答。我明白錯誤處理程序如何工作的邏輯..我想出於某種原因,我不得不調用函數INCASE它有一個錯誤,否則當出現錯誤時它不會顯示錯誤。現在我已經對它進行了排序,但是如何我可以告訴PHP在哪裏顯示錯誤? – Clarkey 2012-07-07 12:05:23

+0

我看到你的改變,以迴應

,這是有道理的。謝謝你的幫助哥們! – Clarkey 2012-07-07 12:08:09