RSS | Archive | Random | E-mail

About

Hi, I'm Christian Castelli, a 28 years old italian programmer located in Pisa (Italy). Here I post small snippets of code which can be useful in my work.

Links

Codepuzzling main site
Development site
ByteStrike italian blog
Follow me on Twitter

My Life Style

while(passion) {
  try {
    myLife.run();
  }catch(LifeExceptions) {  
    stronger++;
    continue;
   }
}

Following

8 July 09

[PHP] How to catch warnings inside a script

Put the case you’re doing a FTP upload of a file. If the transmission fails for whatever problem, you’ll receive a warning but your app won’t stop, you’ll only be able to see that an error occurred. So you can define an error handler to catch those warnings:

// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    switch ($errno) {
	case E__ERROR:   echo "FATAL ERROR ".
                "[line: $errline] $errstr in $errfile\n"; 
                 break;
	case E_WARNING:  echo "WARNING [line: $errline]". 
                $errstr." in $errfile\n"; 
                break;
	case E__NOTICE:  echo "NOTICE [line: $errline] $errstr ".
                in $errfile\n";  
                break;
	default: echo "UNKNOWN ERROR: [line: $errline] $errstr in $errfile\n"; 
    }

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

set_error_handler("myErrorHandler");

$a = 0;    $b = 2;
$c = $b/$a;
echo $c;   // not outputted
This will raise a warning:
WARNING [line: 21] Division by zero in /var/www/temp/warning.php

Tags: php debugging
Comments (View)
  1. codepuzzling posted this
blog comments powered by Disqus
Themed by Hunson. Originally by Josh