PHP alternatives to handling malformed input instead of throwing errors

LomiEmigo

New Member
There are many functions (well most really) in the PHP language that get all upset and throw warnings and notices when they don't like something about their input - rather than just returning \[code\]FALSE\[/code\] (though they do that too).One place this is really common is in the GD and string functions. They are very particular about their arguments and it's really easy for user input to fail meeting their standards.For example, a user uploads a image that is corrupt (intentionally or unintentionally). Resulting in warnings from the GD library.So far there are only three ways I have found to silence PHP on this issue:
  • Change your error reporting setting in the ini or at runtime (yuck).
  • Suppress errors with the slow \[code\]@\[/code\] symbol.
  • Change error reporting right before/after the function:
like so:\[code\]$errorlevel=error_reporting();error_reporting($errorlevel & ~E_NOTICE);//...code that generates noticeserror_reporting($errorlevel);\[/code\]Naturally, the second two choices just make me sick. Which leaves me using 1) and toning down the PHP error settings. However, I want PHP to be in strict mode so that while I'm working I can catch logic bugs and bad form that might creep into my code. However, I don't want to have random errors thrown when PHP doesn't like something.So is there any way to separate errors that are from malformed arguments (bad input) from errors that are from bad programming? For example:[*]If a user image is invalid just return FALSE and I'll deal with it. I don't need warnings.[*]If my passing of an image resource to print function is invalid throw warnings.
 
Back
Top