Jump to content

Archived

This topic is now archived and is closed to further replies.

mynameisrich

What's the "@" operator?

Recommended Posts

Ok, posting here is a last resort. (Apparantly you can't search for the string "@" with Google or php.net's search box. It matches everything.)

What does the '@' operator mean? I've seen it in scripts and can't figure it out. The online documentation at php.net lists '@' as an operator in the order of precedence, with a link to the Types pages, but none of the Types pages has any mention of it. Niether do any of the other language syntax reference pages. (I know. I looked through them all!)

I've played around with it on the php command line, and as far as I can tell, it doesn't actually do anything.

Sample code I saw with it:

[code]if (!$result = @ mysql_query ($query, $connection))
     showerror();
[/code]
Any help?

Thanks!

Share this post


Link to post
Share on other sites
It is the error supresor.

Share this post


Link to post
Share on other sites
The @ sign suppresses errors. If there is an error with something the script will continue to run.

you should never really use the @ operator because you will never get errors outputed on the screen and you will be sitting there wondering why you have nothing showing on your page.

Some people use them just so the clients do not see errors and page names on public sites. I guess this is fine, but while developing your web site you should not be using the @ sign.

Ray

Share this post


Link to post
Share on other sites
Thanks!

So in the example I gave, if an error were generated by mysql_query, what would happen? Would $result be set to the error that was raised? (Which would then evaluate to false and cause the "showerror()" function to be called?)

Share this post


Link to post
Share on other sites
No! The @ sysbol supresses the errors. Meaning no error is shown and allows the script to continue running as though there is no error.

take this script for example:
[code]<?php

if($_GET['var'])
{
    echo $_GET['var'];
}

?>
<a href="?var=hello">Say hello</a>[/code]
When you run that script for the first time, you'll get the following notice error:
[code]Notice: Undefined index: var in C:\server\www\test.php on line 3[/code]If you placed an @ just before $_GET the error message will not be shown. ie:
[code]if(@$_GET['var']))[/code]
This is error suppression. It doesn't affect what a variable contains it just stops an error message being displayed.

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]you should never really use the @ operator[/quote]

I would never say you should not use it, and more of say you should use it in as most places as possible.

Say a connection to a database, you will definitly want to use it there and use your own error output messages on runtime enviroments..

Debugging/development.. yeah you should not use it, but on a release of a script what if the connection to db failed and say username is shown in the error string, thats not information id really want to be showing.

If theres a possible chance an error may arise on an action, I would recommend supressing it and handle the error with your own output.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.