簡體   English   中英

如何在出錯時顯示變量?

[英]How to display variables on error?

在解析PHP中的大文件時,我偶爾會遇到未定義的偏移問題。

如何在發生錯誤時顯示變量,以查看問題是什么?

錯誤發生在我的PHP的這一點上

list($export_date, $application_id, $language_code, $title, $description, $release_notes, $company_url, $suppport_url, $screenshot_url_1, $screenshot_url_2, $screenshot_url_3, $screenshot_url_4, $screenshot_width_height_1, $screenshot_width_height_2, $screenshot_width_height_3, $screenshot_width_height_4,$ipadscreenshot_url_1, $ipadscreenshot_url_2, $ipadscreenshot_url_3, $ipadscreenshot_url_4, $ipadscreenshot_width_height_1, $ipadscreenshot_width_height_2, $ipadscreenshot_width_height_3, $ipadscreenshot_width_height_4 ) = explode($delimiter, $line);

所以我想在偏移錯誤上回顯所有變量,如果沒有錯誤,只需移動到下一條記錄而不做任何事情。

如果explode($delimiter, $line)沒有返回list語句所需的參數,則會出現錯誤,你可以檢查是否是這樣的情況:

$parts = explode($delimiter, $line);
if(count($parts)!=20) { //say your list needs 20 elements
  echo '20 parts expected, got '. count($parts). '<br />';//consider using error_log than echoing data
  var_dump($line, $parts);
} else {
  list($export_date, $application_id, $language_code /*fill others*/) = $parts;
}

你試過set_error_handler嗎? 它允許您編寫將在發生錯誤或警告時執行的函數。 errcontext參數包含所有變量。 例如,您可以在發生時記錄$line ,然后繼續下一行。 請參閱鏈接以獲取示例。

@ aularon的解決方案是一個很好的快速解決方案,但如果您正在尋找長期修復,請嘗試設置錯誤處理程序。 我猜你並沒有真正得到錯誤,而是一個警告。 你可以這樣做:

(見: http//us2.php.net/manual/en/function.set-error-handler.php

function myErrorHandler($errno, $errstr, $errfile, $errline, $symbols)
{
    if (!(error_reporting() & $errno)) {
    // This error code is not included in error_reporting
    return;
    }

    switch ($errno) {

    case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        vardump($symbols['line']); // untested but this should give you the line as set when the error was raised
        break; // maybe die or exit here instead
    }

    /* Don't execute PHP internal error handler */
    return true;
}

您可能希望在開始循環之前使用set_error_handler(),然后在它之后立即執行restore_error_handler(),這樣您就不會為整個應用程序或腳本設置模糊錯誤處理程序。

我有兩種方法:

第一:

您可以將這些值存儲在臨時數組中並計算項目,如果少於24,則出現問題!

$tmp_arr = explode($delimiter, $line);
if(count($tmp_arr) < 24) {
  print_r($tmp_arr); // gives you a nice output
}
else
{
  list($export_date, $application_id, $language_code, $title, $description, $release_notes, $company_url, $suppport_url, $screenshot_url_1, $screenshot_url_2, $screenshot_url_3, $screenshot_url_4, $screenshot_width_height_1, $screenshot_width_height_2, $screenshot_width_height_3, $screenshot_width_height_4,$ipadscreenshot_url_1, $ipadscreenshot_url_2, $ipadscreenshot_url_3, $ipadscreenshot_url_4, $ipadscreenshot_width_height_1, $ipadscreenshot_width_height_2, $ipadscreenshot_width_height_3, $ipadscreenshot_width_height_4 ) = explode($delimiter, $tmp_arr);
}

如果您不喜歡臨時數組,可以計算分隔符(在我看來不太好)

if(substr_count($line, $delimiter) < 23) {
  // less than 24 fields!
  print_r(explode($delimiter, $tmp_arr));
} 
else
{
  // everything alright!
  list($export_date, $application_id, $language_code, $title, $description, $release_notes, $company_url, $suppport_url, $screenshot_url_1, $screenshot_url_2, $screenshot_url_3, $screenshot_url_4, $screenshot_width_height_1, $screenshot_width_height_2, $screenshot_width_height_3, $screenshot_width_height_4,$ipadscreenshot_url_1, $ipadscreenshot_url_2, $ipadscreenshot_url_3, $ipadscreenshot_url_4, $ipadscreenshot_width_height_1, $ipadscreenshot_width_height_2, $ipadscreenshot_width_height_3, $ipadscreenshot_width_height_4 ) = explode($delimiter, $line);
}

!注意! 24個字段只有23個分隔符! ;)

第二種方法:

由於Undefined Offset問題只是來自PHP的“通知”,您可以編寫一個錯誤處理程序來捕獲通知。

見: http//www.codeunit.co.za/2009/09/09/php-how-to-catch-a-script-warning-or-notice/

但這個可能有點矯枉過正;)

最好的祝福

西蒙

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM