Page 1 of 2
Why is there @ in front...
Posted: Mon Mar 20, 2006 3:56 am
by eyespark
I was looking in some code and saw this:
Code: Select all
if (@$_POST['password'] == $admin_pass) {something}
Why is there @ in front of the POST variable?
Thanks
Posted: Mon Mar 20, 2006 3:59 am
by Benjamin
It means supress error messages.
Posted: Mon Mar 20, 2006 4:06 am
by jayshields
agtlewis is correct.
In this situation the programmer has used the @ symbol to make sure PHP doesn't throw an undefined variable error if there was no POST data sent to the page.
In my opinion, doing this is very sloppy/lazy, I don't think the @ symbol should ever be used. The programmer could've just used
Code: Select all
if(isset($_POST['whatever']) && $_POST['whatever'] == 'something') { /* ... */ }
Posted: Mon Mar 20, 2006 4:16 am
by s.dot
@ sometimes has a valid reason to be used
this example wasn't a very good one, it was poor programming
Posted: Mon Mar 20, 2006 7:14 am
by shiznatix
valid example of a time when you should use teh @ sign
Code: Select all
@$con = mysql_connect('user', 'pass', 'host')
if (!$con)
$con = mysql_connect('user2', 'pass2', 'host2');
incase you have a situation where you have identical databases on 2 severs or somtin like that, instead of giving a 'cannot connect to mysql' you just go to the second sever!
Posted: Mon Mar 20, 2006 7:51 am
by jayshields
Wow, nice idea. Never thought of that.
But shouldn't the @ be before mysql_connect() instead of before $con?
Posted: Mon Mar 20, 2006 10:08 am
by John Cartwright
jayshields wrote:Wow, nice idea. Never thought of that.
But shouldn't the @ be before mysql_connect() instead of before $con?
Correct.. it is
basically used to customize your error if there is one.
Posted: Mon Mar 20, 2006 10:13 am
by Chris Corbyn
Note that in PHP5 we have try/catch which is nicer still

Posted: Mon Mar 20, 2006 10:17 am
by John Cartwright
d11wtq wrote:Note that in PHP5 we have try/catch which is nicer still

I personally find that use of error reporting kinda unreadable when overused, and some applications use it for EVERYTHING, cough Zend Framework. I know I've heard other people agree, but it is just an opinion.
Posted: Mon Mar 20, 2006 10:19 am
by Chris Corbyn
Jcart wrote:d11wtq wrote:Note that in PHP5 we have try/catch which is nicer still

I personally find that use of error reporting kinda unreadable when overused, and some applications use it for EVERYTHING, cough Zend Framework. I know I've heard other people agree, but it is just an opinion.
It makes it easy to log exceptions to a database and hide them from users. This way you can pick up on post-release bugs in a system

Posted: Mon Mar 20, 2006 10:21 am
by John Cartwright
d11wtq wrote:Jcart wrote:d11wtq wrote:Note that in PHP5 we have try/catch which is nicer still

I personally find that use of error reporting kinda unreadable when overused, and some applications use it for EVERYTHING, cough Zend Framework. I know I've heard other people agree, but it is just an opinion.
It makes it easy to log exceptions to a database and hide them from users. This way you can pick up on post-release bugs in a system

so can
http://ca.php.net/set_error_handler (sorta

) ? I was strictly talking about readability.
Posted: Mon Mar 20, 2006 1:38 pm
by eyespark
jayshields wrote:agtlewis is correct.
In this situation the programmer has used the @ symbol to make sure PHP doesn't throw an undefined variable error if there was no POST data sent to the page.
In my opinion, doing this is very sloppy/lazy, I don't think the @ symbol should ever be used. The programmer could've just used
Code: Select all
if(isset($_POST['whatever']) && $_POST['whatever'] == 'something') { /* ... */ }
Aaah, yes. I always use that method. Thanks for all your input guys!
Posted: Mon Mar 20, 2006 3:13 pm
by onion2k
shiznatix wrote:valid example of a time when you should use teh @ sign
Code: Select all
@$con = mysql_connect('user', 'pass', 'host')
Wouldn't that suppress an error on the variable assignment rather than the mysql_connect()?
Posted: Mon Mar 20, 2006 3:34 pm
by RobertGonzalez
onion2k wrote:shiznatix wrote:valid example of a time when you should use teh @ sign
Code: Select all
@$con = mysql_connect('user', 'pass', 'host')
Wouldn't that suppress an error on the variable assignment rather than the mysql_connect()?
I think you're right onion. I thought the use of this was supposed to be like this...
Code: Select all
<?php
$con = @mysql_connect('user', 'pass', 'host')
?>
... so if the mysql_connect (or whatever function you are calling) returns an error, that error will be quieted.
On a side note, isn't anyone else around here ready strangle their IT administrators for using a (as in one) proxy server that serves internet connections to 400+ users? Man, my download rate right now is just under the speed of paint drying... under water.
Posted: Mon Mar 20, 2006 4:14 pm
by shiznatix
sorry guys, I made a typo. will you ever forgive me!!!???
/me crys himself to sleep tonight.