Jump to content


Photo

What's the "@" operator?


  • Please log in to reply
5 replies to this topic

#1 mynameisrich

mynameisrich
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 10 April 2006 - 03:50 PM

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:

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

Thanks!

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 10 April 2006 - 03:52 PM

It is the error supresor.

#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 10 April 2006 - 03:53 PM

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

#4 mynameisrich

mynameisrich
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 10 April 2006 - 04:04 PM

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?)

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 10 April 2006 - 04:45 PM

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:
<?php

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

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

#6 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 10 April 2006 - 05:41 PM

[!--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.
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users