Page 1 of 1

PHP debugging

Posted: Fri Aug 22, 2008 2:13 pm
by joatmon
There was an awesome instructional page written by ianmac regarding debugging PHP code on Eclipse, but it appears to have disappeared. Is there a new URL for this page ianmac? Or did you take it down permanently. I hope not because it was a great resource and helped me out tremendously.

thanks!

Re: PHP debugging

Posted: Fri Aug 22, 2008 7:47 pm
by Gergo Erdosi

Re: PHP debugging

Posted: Sat Aug 23, 2008 7:32 pm
by joatmon
Very nice. Thanks!

Re: PHP debugging

Posted: Mon Aug 25, 2008 6:41 pm
by chaosprime
Just because I try to make this available to anyone who might care: you might get some mileage out of this snippet that makes PHP produce stack traces on errors and warnings, instead of useless final-location-only errors:

Code: Select all

<?php
function process_error_backtrace($errno, $errstr, $errfile, $errline, $errcontext) {
    if(!(error_reporting() & $errno))
        return;
    switch($errno) {
    case E_WARNING      :
    case E_USER_WARNING :
    case E_STRICT       :
    case E_NOTICE       :
    case E_USER_NOTICE  :
        $type = 'warning';
        $fatal = false;
        break;
    default             :
        $type = 'fatal error';
        $fatal = true;
        break;
    }
    if(php_sapi_name() == 'cli') {
        echo 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
        foreach(array_reverse(debug_backtrace()) as $item)
            echo '  ' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()' . "\n";
    } else {
        echo '<p class="error_backtrace">' . "\n";
        echo '  Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
        echo '  <ol>' . "\n";
        foreach(array_reverse(debug_backtrace()) as $item)
            echo '    <li>' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()</li>' . "\n";
        echo '  </ol>' . "\n";
        echo '</p>' . "\n";
    }
    if($fatal)
        exit(1);
}

set_error_handler('process_error_backtrace');
?>