How to display variables on error?

I get random Undefined errors while parsing a large file inside PHP.

How can I display variables ONLY when an error occurs to find out what the problem is?

An error occurs at this point in 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 repeat all the variable bias errors, if there is no error, just go to the next record without doing anything.

+3
source share
4 answers

an error will occur if it explode($delimiter, $line)does not return as many parameters as required for the list operator, you can check if this works 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;
}
0
source

set_error_handler? , . errcontext . , , $line, , . . .

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() , script.

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

! 23 24 !;)

:

Undefined Offset - "" PHP, , .

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

;)

0

Source: https://habr.com/ru/post/1763616/


All Articles