newbie question why is '@' used ?

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

byenary
Forum Newbie
Posts: 10
Joined: Mon Apr 03, 2006 6:15 am

newbie question why is '@' used ?

Post by byenary »

Hello i have basic knowledge of php syntax but i was analyzing some code and had code like this
$result = @mysql_query("SELECT folderkey FROM viduser WHERE serial = '".$_REQUEST['serial']."'");
and
@fwrite($fh_p, " <play fn='".$fn."' time='".$time."' slide='".$slide."' type='".$type."' />\r\n");

What does the @ mean in tese syntaxes ?

Thx
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Never use @. Ever.
Supper Baby
Forum Commoner
Posts: 27
Joined: Tue Aug 01, 2006 11:33 pm

Post by Supper Baby »

@ is using to pass any error maybe you get.

When you use @ the script will not print any error for the exact comand
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Supper Baby wrote:When you use @ the script will not print any error for the exact comand
It will supress the error generated by that particular line no matter how many other lines of code that line may invoke.

For instance you could have something like this:

Code: Select all

@$application = new Application();
Which would start an entire application's worth of code (thousands of lines) and all the errors would be supressed by that one @.

The reasons why you should never use it are:
  • If you are using it to suppress 'undefined' type errors there are usually more readable and better performing alternatives:

    Code: Select all

    isset() array_key_exists() property_exists() method_exists() class_exists() file_exists()
  • If you are using it to suppress errors with external operations such as files, sockets or databases you probably shouldn't be using it and should in fact use something like mysql_error() (for mysql) or compare the return value against !== false or !== null. or die() is commonly used as well.
  • You can reduce and often eliminate the risk of an operation failing by testing something first like

    Code: Select all

    is_file() is_readable() is_callable() is_int() is_array() instanceof....
  • If you want to supress all errors there's

    Code: Select all

    error_reporting(0);
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

ole wrote:Never use @. Ever.
Actually there ARE times when it should be used. :)

You should definitely use it in the mysql example he is giving as long as you have an error check after. If you do not use the @ to suppress the error message then the page will break in a horrible fashion and look like crap.

If you do use the @ to suppress the error message you then need to have an error check right after. You can then redirect to a page that processes the error, logs it and displays information to the client. IE: "I am sorry but the page you requested is not available. Please try again later. The Admin has been notified of this error. Thank You"

Letting a page break horribly with a php error message is a little amateurish. :)
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Call in the mods!

I sense another '@ Debate'
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Here are a couple of examples using mysql...

Code: Select all

<?php

$this->connectionId = @mysql_connect( $this->host, $this->username, $this->password );

if ($this->connectionId === false)
{
	if ($fn = $this->raiseErrorFn) 
		$fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this);
	return false;
}

?>
In the above example we suppress the PHP error when trying to connect to the database server. By doing this we can then do a check to see if a connection was made. If it wasn't we return an error flag that can be processed.

Code: Select all

<?php

$resultId = @mysql_query( $this->sql, $this->connectionId );

if ($resultId === false) { // error handling if query fails
	if ($fn = $this->raiseErrorFn)
		$fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
	return $false;
} 

?>
We can do the same thing with a query. We suppress the PHP error and then check to see if the query actually executed and return an error flag if it wasn't.

You can basically do the same thing with many other functions in PHP such as any of the file functions. It allows the programmer to be in control of all error processing instead of just letting the site break.
Last edited by AKA Panama Jack on Sat Aug 26, 2006 10:35 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No real need to use it the query method. Just have a method isAlive() which you check returns true before running the query. mysql_query() won't puke on your pages if the query is wrong.... it just returns false.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Error suppression slows down your apps. I am of the opinion that you can successfully manage potential errors through code rather than relying on hushing them up. I am not a fan of a band-aid approach.

As for the topic of a debate, this thread is progressing well. I would just ask that we keep it logical and about the issue of the performance and use of error suppression, not about personal issues with the way others do things. That will get this thread shut down quickly.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

You definately need to use it with a query method. You are not just checking to see if the connection is alive you are checking to see if the query actually PROCESSED. If you have a malformed query this check can return information about the query and what was wrong with it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You know, AKAPJ has made me remember something. My DB class interface uses error suppression in the class. I DO NOT use it anywhere else in any other code that I develop, but I have used it (and still do as a matter of fact) in my DB classes. But that is the only area.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

AKA Panama Jack wrote:You definately need to use it with a query method. You are not just checking to see if the connection is alive you are checking to see if the query actually PROCESSED. If you have a malformed query this check can return information about the query and what was wrong with it.
So why do you need error surpression again? Can't you just check if mysql_query returns true or false? Like I say, it doesn't display errors with the query you run, it returns a boolean false. You need mysql_error for that. This is why so many newbies get confused with their code not working... mysql_query doesn't look like anything went wrong since it doesn't display errors.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Everah wrote:Error suppression slows down your apps. I am of the opinion that you can successfully manage potential errors through code rather than relying on hushing them up. I am not a fan of a band-aid approach.
I think the debate is still open on if error suppression slows down PHP. It may have on the early, very SLOW 3.xx versions of PHP but on the later PHP 4 and 5 it doesn't look like it. Removing the @ doesn't seem to increase the number of executions per second.

I personally don't see it as a band-aid but another tool for programmers to allow them far more control over how PHP reacts to different situations.

Admittedly, there are programmers who use the @ liberally in their code and have absolutely no error checking afterwards. Now that I DO consider a band-aid approach. That shows the programmer had errors and didn't want to take the time to find out what they were and just made them go away. Very bad programming. :)

But if you couple error suppression with error processing you can have a powerful combination that is a very useful programming tool.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

d11wtq wrote:This is why so many newbies get confused with their code not working... mysql_query doesn't look like anything went wrong since it doesn't display errors.
It might returm an E_WARNING error.
Post Reply