Page 1 of 3
newbie question why is '@' used ?
Posted: Sat Aug 26, 2006 4:23 am
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
Posted: Sat Aug 26, 2006 4:25 am
by Oren
Posted: Sat Aug 26, 2006 5:41 am
by Ollie Saunders
Never use @. Ever.
Posted: Sat Aug 26, 2006 6:36 am
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
Posted: Sat Aug 26, 2006 6:56 am
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
Posted: Sat Aug 26, 2006 10:08 am
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.

Posted: Sat Aug 26, 2006 10:17 am
by LiveFree
Call in the mods!
I sense another '@ Debate'
Posted: Sat Aug 26, 2006 10:28 am
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.
Posted: Sat Aug 26, 2006 10:32 am
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.
Posted: Sat Aug 26, 2006 10:35 am
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.
Posted: Sat Aug 26, 2006 10:38 am
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.
Posted: Sat Aug 26, 2006 10:44 am
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.
Posted: Sat Aug 26, 2006 10:45 am
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.
Posted: Sat Aug 26, 2006 10:46 am
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.
Posted: Sat Aug 26, 2006 10:51 am
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.