views:

40

answers:

4

I am getting occasional Undefined offset issues when parsing a large file inside PHP.

How can I display the variables ONLY when the error occurs, to see what the issue is?

The error is occuring at this point of my 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);

So I would like to echo all the variables on the offset error, if no error, just move onto the next record without doing anything.

A: 

the error will occur if explode($delimiter, $line) didn't return as much parameters as the list statement requires, you can check if that's the case like this:

$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;
}
aularon
thank you this if perfect, if it errors it means my input data is corrupt, so I want to ignore it.
kitenski
A: 

Did you try set_error_handler? It allows you to write a function that will be executed when an error or warning occurs. The errcontext parameter contains all the variables. You could for example log $line when it happens and then continue to the next line. See the link for examples.

Ishtar
A: 

@aularon's solution is a great quick solution but if you are looking for a long-term fix, try setting the error handler. I'm guessing you aren't really getting an error, but rather a warning. You could do something like this:

(see: 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;
}

You may want to set_error_handler() right before you start your loop, then restore_error_handler() right after it so you don't end up with a fuzzy error handler for the entire app or script.

Hans
A: 

Hello,

I have two approaches for you:

First:

You could store these values in a temporary array and count the items, if there are less than 24, something went wrong!

$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 you don't like the temporary array, you could count the delimiters (not as good in my opinion)

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);
}

!Attention! you only have 23 delimiters for 24 fields! ;)

Second Approach:

Since the Undefined Offset issue is just a "Notice" from PHP you could write an error handler which catches the notice.

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

But this one maybe a little overkill ;)

Best Regards

Simon

sled