Why is there @ in front...

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

eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

Why is there @ in front...

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

It means supress error messages.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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') { /* ... */ }
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

@ sometimes has a valid reason to be used

this example wasn't a very good one, it was poor programming
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Wow, nice idea. Never thought of that.

But shouldn't the @ be before mysql_connect() instead of before $con?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by Chris Corbyn »

Note that in PHP5 we have try/catch which is nicer still :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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 ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 :wink:) ? I was strictly talking about readability.
eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

Post 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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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()?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

sorry guys, I made a typo. will you ever forgive me!!!???

/me crys himself to sleep tonight.
Post Reply