What does @ do?

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

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

What does @ do?

Post by Luke »

I have seen @ used before functions and a few other things I think. What does it do?
conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Re: What does @ do?

Post by conthox »

One armed space goat wrote:I have seen @ used before functions and a few other things I think. What does it do?
If a function fails to exec, @ prevents the error message to be displayed.
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

Mysql functions it suppresses the built in error codes

like @mysql_query

can be used in a IF statement to print your own errors
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

It surpresses PHP's Exception error messages for the command it is preceding.

I can't find the page for it atm, but it only supresses non-fatal errors (E_WARNING and E_NOTICE errors and E_STRICT in PHP5), if it is E_CORE_*, E_COMPILE_*, E_ERROR or E_PARSE error (all fatal errors), then you will still see an error message.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: What does @ do?

Post by redmonkey »

One armed space goat wrote:What does it do?
It hangs around waiting for people to discover it and abuse it's functionality.

It can be useful, but it's also probably one of the most mistreated operators within PHP
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: What does @ do?

Post by Luke »

redmonkey wrote:
One armed space goat wrote:What does it do?
It hangs around waiting for people to discover it and abuse it's functionality.

It can be useful, but it's also probably one of the most mistreated operators within PHP
What is its proper use then? and how do people use it inproperly? What could I do with this?
conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Re: What does @ do?

Post by conthox »

One armed space goat wrote:
redmonkey wrote:
One armed space goat wrote:What does it do?
It hangs around waiting for people to discover it and abuse it's functionality.

It can be useful, but it's also probably one of the most mistreated operators within PHP
What is its proper use then? and how do people use it inproperly? What could I do with this?
I would like to know that too.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

It is as mentioned previously, it is used for error supression, the problem that I have noted with it is that many people seem to discover it, then start applying it to every error the come across rather than attempting to resolve the error at the root of the problem. This leaves them wondering why A) their script doesn't work and B) why they aren't seeing any error messages.

I do use it myself, but very rarely do I have cause to use it without and additional error trap. Some classic uses are...

A simple database connection...

Code: Select all

$conn_id = @mysql_connect('blah', 'blah', 'blah');

if (!$conn_id)
{
  //throw some error
}
Another quite useful one is including of files...

Code: Select all

$loaded = @include('somefile.php');

if (!$loaded)
{
  // throw some error
}
That one there is quite handy as normally if include fails script execution continues and an error is generated. If you need/want script execution to halt if the file does not get included then you have two options. Do as above and you can gracefully fall over, or use require which terminates script execution with and ugly warning thrown out to the buffer.

There are many uses for it and it if used properly is extremly useful.

[EDIT] I have also noted that my motor skills appear to be failing tonight, apologies for the numerous typos and missing words.[/EDIT]
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

redmonkey wrote:I have noted with it is that many people seem to discover it, then start applying it to every error the come across rather than attempting to resolve the error at the root of the problem.
That is an absolutely perfect description of the situation. I have the same issue with html designers and html compliance, and the list expands from there.

Excellent description.

A good "where would I use it properly" example would be a place where I knew I would throw an error, but I disagreed with the parser. The only actual time I've encountered it, and felt justified/correct in using it was in adodb in a very specific combination involving the xml parser, which isn't properly maintained.

Its definitely a rare thing for me to use it without an intention of removing it very soon after using it.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

A good example of its use is in forums. For example, resizing pictures that are coming from your forum users. Whos to say that their picture is actually a valid url? When getting the size of that image, if PHP can't find the image, it will produce an hour.

So, using @getimagesize() will supress my error log from being overloaded with these errors. :)
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
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Great... you guys are awesome... thanks for the examples.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

one function I always use it on is mysql_close().
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

now here's the real question:

what does it mean in a sample like this?

Code: Select all

function @ myFunction()
{
    // some stuff
}
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Burrito wrote:now here's the real question:

what does it mean in a sample like this?

Code: Select all

function @ myFunction()
{
    // some stuff
}
It means you'll have a parse error in your code.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

redmonkey wrote:
Burrito wrote:now here's the real question:

what does it mean in a sample like this?

Code: Select all

function @ myFunction()
{
    // some stuff
}
It means you'll have a parse error in your code.
probably.... :wink:
Post Reply