[PHP] Error Suppression with '@'
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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
Hello Justin, I would guess that this is mostly performance and memory related. When, for instance, trying to iterate an array, or a directory with files, using @ on all function calls, it will attempt to go through the array, attempting to allocate memory for each item, but when finding it empty/broken, it leaves the loop and goes on to the next statement in the code. From what I can tell, PHP handles memory efficiently, although I wouldn't hazard this when deploying a larger site using this type of approach. Especially if the code inside a larger if..else block was performed on an object that was created with @function() call, and then all the consecutive calls to functions operating on that object also used @. This would take up both CPU time and memory, possibly creating leaks, based on how efficiently PHP would handle the operations on a non-existant object. A rule of thumb is to instead use empty() and is_array() and such functions to discern if the object you are going to handle indeed is of the type you wish it to be, that saves you both time and effort when trying to debug something in the future. The empty() function goes a long way here, unless dealing with integers, since empty() will return true if the value is 0, which might not be desireable. I hope this helps you in your argument against your fellow developer. :] Regards, Torgny 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
On 8/2/05, Justin Burger [EMAIL PROTECTED] 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; Any chance of revealing his identity - so that I can make sure that I don't sub-contract him any work. 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? It's bad in that your system normally gives you an error when you do something wrong. Sometimes your code can recover from errors, but your code runs better if the errors aren't there. In production code, you don't want the errors to show up on screen, mainly because you don't want to expose the code you are using on your back end. You fix this by modding the correct parameter in php.ini ( display_errors IIRC ) php.net does not have much information about this. It seems like suppressing errors, rather then catching them is problematic. PHP displays errors for a reason. It is both egotistic, and foolish to assume that your code will always be perfect. Errors enlighten you when your code is not perfect, and give you the opportunity to perfect it. You can achieve a similar effect by setting display_errors to the correct setting. Thanks Again. Justin. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
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. This is a Bad Idea. A Very Bad Idea actually. If he's just looking to hide error output, he should at least edit php.ini to log to an error file. Otherwise, debugging his code would be horrible. The @ is total slop. Matt Darby -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
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. I'm going to partially disagree with some of the responses you get on this by saying that it doesn't have to be sloppy or a bad idea. I use the '@' symbol on _some_ of my function calls (mainly MySQL stuff), but only because I have my own error handling system in place. Some will say that if you want to control what is being printed to the screen, modify the php.ini file, but I don't fully subscribe to that. In production, I do turn off notices, but that's about it. For me, it boils down to my wanting to control the verbatim of the error; ie make the error messages a bit more end user friendly. The reason I don't like suppressing this in the ini file is it becomes a pain in the ass to try and debug a customer's problem (with the customer on the phone), if no error messages are showing on the screen for him/her. -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
Justin Burger wrote: Does suppressing the error only suppress it from the screen, or does it ignore the error? ie: is the error still logged? Please reply to the list. I don't know if it still logs the error (assuming you have error logging turned on). -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
Does suppressing the error only suppress it from the screen, or does it ignore the error? ie: is the error still logged? On Aug 2, 2005, at 12:18 PM, John Nichel wrote: 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. I'm going to partially disagree with some of the responses you get on this by saying that it doesn't have to be sloppy or a bad idea. I use the '@' symbol on _some_ of my function calls (mainly MySQL stuff), but only because I have my own error handling system in place. Some will say that if you want to control what is being printed to the screen, modify the php.ini file, but I don't fully subscribe to that. In production, I do turn off notices, but that's about it. For me, it boils down to my wanting to control the verbatim of the error; ie make the error messages a bit more end user friendly. The reason I don't like suppressing this in the ini file is it becomes a pain in the ass to try and debug a customer's problem (with the customer on the phone), if no error messages are showing on the screen for him/her. -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
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.
Re[2]: [PHP] Error Suppression with '@'
Hello Justin, Tuesday, August 2, 2005, 8:43:09 PM, you wrote: JB Does suppressing the error only suppress it from the screen, or JB does it ignore the error? JB ie: is the error still logged? It will ignore it totally, it doesn't even make it as far as the log files - which is why in most cases it's a bad thing to use. When it comes to the mysql/i functions however I will suppress the error and use my own error checking to avoid blank pages / unsightly warnings. Best regards, Richard Davey -- http://www.launchcode.co.uk - PHP Development Services Zend Certified Engineer I do not fear computers. I fear the lack of them. - Isaac Asimov -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error Suppression with '@'
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. in short what you monkey is doing sucks - if it was a good idea php would be designed to output or log no errors at all... ever ... funnily enough that's not they way it's designed. knowing how to handle errors properly AND doing it is IMHO one of the main differentiators between amateur and professional programmers - of course the true wizards among us know when they can break the rules (e.g. hack up some nasty little script that scratches an itch knowing full well it 'breaks the rules') :-) if I were you I would give serious weight to the words of John Nichel on this matter - he might not admit it but he really knows where his towel ;-) Thanks Again. Justin. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Error suppression operator (@)
I would like to know, whether using @ is a good practice. For example, I have an array of unknown length $array. Is it all right write something like this: @list($first, $second) = $array; or is it better to do length check? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error suppression operator (@)
On 5/3/05, GamblerZG [EMAIL PROTECTED] wrote: I would like to know, whether using @ is a good practice. I try not to use it much, but when I do I back it up with checking to see if an error really occured. I use it for file handles, database handles, stuff that I really expect to break sometimes. I don't ever use it to assist with sloppy coding style, not defining variables and such. In fact I always code with error_reporting( E_ALL ) until I take the code into production. For example, I have an array of unknown length $array. Is it all right write something like this: @list($first, $second) = $array; I go with $array[0], $array[1] and such. Or maybe while( list( $k, $v ) = each( $array ) ) { } -- Greg Donald Zend Certified Engineer http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error suppression operator (@)
Greg Donald wrote: On 5/3/05, GamblerZG [EMAIL PROTECTED] wrote: I would like to know, whether using @ is a good practice. I try not to use it much, but when I do I back it up with checking to see if an error really occured. I use it for file handles, database handles, stuff that I really expect to break sometimes. I don't ever use it to assist with sloppy coding style, not defining variables and such. In fact I always code with error_reporting( E_ALL ) until I take the code into production. Greg makes some good points. in short don't use @ lightly, in theory you are never required to use it at all. in some situations you may have an intensive/heavy function or loop which access array items (inside the loop) which may or may not be set... in such cases you may find you want/need to try and optimize foreach($arr1 as $a) { // if (isset($arr2[ $a ])) { if (@$arr2[ $a ]) { // ... } // ... } this example assumes that $arr2 is an array and that every item you are checking will cast to boolean true (e.g. the value 0 will cast to false) -- so on a few occasions you can 'cheat' with the @ and grab a cycle or 2, but be very careful what you are doing. have fun :-) For example, I have an array of unknown length $array. Is it all right write something like this: @list($first, $second) = $array; I go with $array[0], $array[1] and such. Or maybe while( list( $k, $v ) = each( $array ) ) { } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Error suppression operator (@)
Pretty much the only time i use it is form processing... so i don't get a bunch of errors when someone doesn't fill out a (non-required) field.. Also i use it to prefill form data is i have a session running, ie. input name=fname type=text value?php echo @$_SESSION['APP']['fname']; ? / like others have mentioned... if there is no value, the form is blank, otherwise the value is used as its defualt value... pretty neat! Colin On 5/4/05, Jochem Maas [EMAIL PROTECTED] wrote: Greg Donald wrote: On 5/3/05, GamblerZG [EMAIL PROTECTED] wrote: I would like to know, whether using @ is a good practice. I try not to use it much, but when I do I back it up with checking to see if an error really occured. I use it for file handles, database handles, stuff that I really expect to break sometimes. I don't ever use it to assist with sloppy coding style, not defining variables and such. In fact I always code with error_reporting( E_ALL ) until I take the code into production. Greg makes some good points. in short don't use @ lightly, in theory you are never required to use it at all. in some situations you may have an intensive/heavy function or loop which access array items (inside the loop) which may or may not be set... in such cases you may find you want/need to try and optimize foreach($arr1 as $a) { // if (isset($arr2[ $a ])) { if (@$arr2[ $a ]) { // ... } // ... } this example assumes that $arr2 is an array and that every item you are checking will cast to boolean true (e.g. the value 0 will cast to false) -- so on a few occasions you can 'cheat' with the @ and grab a cycle or 2, but be very careful what you are doing. have fun :-) For example, I have an array of unknown length $array. Is it all right write something like this: @list($first, $second) = $array; I go with $array[0], $array[1] and such. Or maybe while( list( $k, $v ) = each( $array ) ) { } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php