Example:
I was working on a HORRIBLE piece of code for a cart app. The original
programmer had a line like this:
$result = mysql_query( "SELECT * FROM user_logins WHERE
cookie='".$cookie."'" );
Where $cookie is a session id stored in a cookie (what else? ;). The
problem was, he had some faulty code that was not always properly
setting the cookie. So then he went and did the following:
$customer_id=mysql_result($result,0,"id");
Problem is, $cookie='' but there are no entries in the user_logins table
where cookie=''. So, the query is empty and trying to pull the customer
ID gave the nice little warning because E_ALL was on:
*Warning*: mysql_result(): Unable to jump to row 0 on MySQL result index
3 in *cart.php* on line *26*
So customers get confused because of the warning and on top of that they
have no items in their cart because customer_id is used to look up temp
carts (and it wasn't set). He couldn't figure out why it was happening
and he didn't have logging turned on, so all he had was Billy Bob saying
"My cart is gone. There was an error. I don't know what it was. I
didn't write it down." So then he decided to put an @ in front of the
offending lines. Customers no longer get an error. Excellent.
But wait! Three months down the road, we have the problem of the
vanishing carts. So third-party programmer comes in, turns on logging
so I can see the stupid errors and figure out what's going on, but
errors have been repressed! This is not helpful to anyone, I then have
to go through and figure out whether it will break something by removing
those stupid @'s (there were a LOT of them).
The problem wasn't that he was using @, but that he was not properly
checking for errors, and used @ to suppress bugs he couldn't duplicate
or couldn't figure out how to fix. In short, if you don't want the
customer to see the error, turn of reporting and turn on logging.
Anyone trying to debug your code three years later will thank you.
my 2c.
kgt
Justin Burger wrote:
Good Morning,
I was having a discussion with a fellow PHP Developer this morning and he
mentioned that he put's an '@' sign in front of all function calls, and
every time he accesses an array;
I know that this is sloppy, and dangerous, but I don't know exactly what
this exposes him to, can any one give me any real world examples of why
this is bad, so I can relate it to his code?
php.net does not have much information about this. It seems like
suppressing errors, rather then catching them is problematic.
Thanks Again.
Justin.