Void().

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Void().

Post 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)?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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))
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You might also be interested in http://de2.php.net/getimagesize
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply