Discuss For the simple things: @ or condition? in the PHP Development forum on Dev Shed. For the simple things: @ or condition? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
But obviously that'll still throw a notice if "foo" isn't defined, so:
$foo = @($_REQUEST["foo")?:"default";
And thus I began to wonder: just how bad is using @ generally considered to squelch errors?
There are definitely places I've seen it used that I would never use it, such as on function calls with a lot of logic behind them and a lot of very valid and important potential errors. Those shouldn't just be ignored.
But what about for things that you *know* the error conditions are limited and functionally painless?
Time spent in forums: 2 Months 3 Weeks 4 Days 18 h 26 m 56 sec
Reputation Power: 1063
this is not as bad as the typical (mis)uses of the @ operator, but it's still a hack, don't you think?
Personally, I prefer clarity and readability over saving a few characters. The first variant is clear: Everybody can understand it immediately. The second variant is rather obscure. Also note that it will apply the default value in case of a falsy value (0, 0.0, '', false, ...).
The thing is: You write your code once, but it usually gets read many times (unless you're programming just for fun without anybody else involved). This means fancy shortcuts usually don't pay off. You may save 2 seconds while you write a code, but 10 people (or whatever) each lose 5 seconds trying to figure out what the f*** this does.
Time spent in forums: 5 Months 2 Weeks 2 Days 4 h 34 m 24 sec
Reputation Power: 9259
If it's at all possible to check for the error condition, do it.
* Not sure if a GET variable is present? isset()
* Not sure if a file exists? file_exists()
* File is readable or writable? is_readable() and is_writable()
There is exactly one circumstance where I would use a @ and that's because there is no reasonable way to check for the error condition: getimagesize() to determine if a file is an image.
Time spent in forums: 2 Months 3 Weeks 5 Days 8 h 34 m 4 sec
Reputation Power: 6300
You can even solve the getimagesize problem with either finfo_fopen() or mime_content_type(), but both require you to hand-code all possible image types rather than relying on the library to figure it out.
Time spent in forums: 5 Days 15 h 54 m 21 sec
Reputation Power: 64
Originally Posted by ManiacDan
Always do it properly, and never ignore errors.
Depends what you consider an error. Even PHP sees those as just Notices and, by default, wouldn't even report them. I just prefer to have them reported on to help ensure every anomaly is accounted for.
That said, I wouldn't consider this ignoring anything either. Turning Notice reporting off altogether would be ignoring it. I'm explicitly choosing to accept that the error can occur and let it happen without explicit prevention.
Similarly, there have been times I've chosen to do nothing after catching an exception because the failure doesn't impact the overall business logic.
Originally Posted by Jacques1
Also note that it will apply the default value in case of a falsy value
Ah, good catch. I'd missed that. Back to the original syntax then...
You write your code once, but it usually gets read many times (unless you're programming just for fun without anybody else involved). This means fancy shortcuts usually don't pay off.
It's actually the readability of the code that I was considering. When I have a line per passed variable that can easily add up to many lines--especially for form ingestion. The original syntax makes lines roughly twice as long and makes for a very blocky segment of code that can be hard to wade through when reading.
Time spent in forums: 2 Months 2 Days 20 h 11 m 10 sec
Reputation Power: 7170
Using the error suppression operator is also a lot less efficient than using an isset or empty statement because PHP still has to invoke its error handling code when you use the error suppression operator even if all the error handling code does is ignore the error.