Page 1 of 1

Void().

Posted: Sun May 20, 2007 12:34 am
by JellyFish
How do I "void()" a line of code?

E.g:

Code: Select all

if (imagecreatefrompng($path))
{
...
it errors when I want it to just return false.

How do I not run this code but have it return true if successful and false it not (i.e if it's not a png file)?

Posted: Sun May 20, 2007 3:12 am
by Chris Corbyn
You may not be able to do what you want without proper error handling using set_error_handler(). However, this may work for you:

Code: Select all

if (@imagecreatefrompng($path))

Posted: Sun May 20, 2007 3:50 am
by Kieran Huggins
I feel dirty every time I suppress errors with @, but sometimes it makes good sense. Be careful not to use it too often, it has many pitfalls.

Posted: Sun May 20, 2007 6:16 am
by stereofrog
In php 5 you can also use the power of exceptions.

Install an error handler that converts php errors to exceptions:

Code: Select all

# somewhere at  the very top of the script

set_error_handler(create_function(
	'$severity, $message, $filename, $lineno',
   '{throw new ErrorException($message, 0, $severity, $filename, $lineno);}'
));
and wrap the "dangerous" pieces into try-catch blocks:

Code: Select all

try {
	fopen('blah');
} catch(Exception $e) {
	// silence
}

Posted: Sun May 20, 2007 10:54 am
by volka
You might also be interested in http://de2.php.net/getimagesize

Posted: Sun May 20, 2007 11:00 am
by Ambush Commander
Try to avoid using @: rather, create a blank function and set it as the error handler. The reason for this more verbose syntax is that if a custom error handler implementation doesn't honor error_reporting(), the errors won't get suppressed.