2012-06-01 16 views
0

如何捕獲所有PHP錯誤並將它們放入字符串var?捕獲所有PHP錯誤並將它們放入字符串var

我想我應該使用set_error_handler()。

我正在使用Ajax + JSON,並希望在字符串中輸出錯誤,然後通過JSON輸出它們。

謝謝。

+0

你爲什麼要這麼做? – markus

+0

我正在使用Ajax + JSON並希望在字符串中輸出錯誤,然後通過JSON輸出它們。 – ihtus

+0

看看php [error_get_last()](http://www.php.net/manual/en/function.error-get-last.php),但你需要追蹤它。 –

回答

0

使用此:

<?php 

try { 

// your code block here 

} catch (Exception $e) { 
    $_my_catch_var = $e->getMessage(); 
} 
1

嗯,這很簡單:

/// Exception handler function 
function yourExceptionHandler($exception) 
{ 
    echo ' 
    <pre> 
    <b>Error</b>: Unhandled '.$exception.' 
    occured <b>'.$exception->getFile().'</b> in line <b>'. 
    $exception->getLine().'</b><br /> 
    </pre>'; 
} 

然後把這個功能給PHP:

/// Assign exception handler function 
set_exception_handler('yourExceptionHandler'); 

將這個代碼在你的PHP的開始腳本。

相關問題