在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);
所以我想在偏移误差上回显所有变量,如果没有错误,只需移动到下一条记录而不做任何事情。
答案 0 :(得分:0)
如果explode($delimiter, $line)
没有返回列表语句所需的参数,则会发生错误,您可以检查是否是这样的情况:
$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;
}
答案 1 :(得分:0)
你试过set_error_handler吗?它允许您编写将在发生错误或警告时执行的函数。 errcontext
参数包含所有变量。例如,您可以在发生时记录$line
,然后继续下一行。请参阅链接以获取示例。
答案 2 :(得分:0)
@ 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(),这样您就不会为整个应用程序或脚本设置模糊错误处理程序。
答案 3 :(得分:0)
我有两种方法:
首先:
您可以将这些值存储在临时数组中并计算项目数,如果小于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/
但是这个可能有点矫枉过正;)
最好的问候
西蒙