Jump to content


Photo

@ before functions name


  • Please log in to reply
9 replies to this topic

#1 ReVeR

ReVeR
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 06 June 2006 - 11:29 AM

Hello
What does @ b4 function name do when the function is called.
Thx

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 June 2006 - 11:32 AM

suppresses errors - so it's not usually a good idea
Legend has it that reading the manual never killed anyone.
My site

#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 June 2006 - 11:34 AM

[!--quoteo(post=380555:date=Jun 6 2006, 07:32 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 6 2006, 07:32 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
suppresses errors - so it's not usually a good idea
[/quote]

But it also hides warnings. So it can be pretty useful
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 June 2006 - 11:56 AM

[!--quoteo(post=380556:date=Jun 6 2006, 07:34 AM:name=play_)--][div class=\'quotetop\']QUOTE(play_ @ Jun 6 2006, 07:34 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
But it also hides warnings. So it can be pretty useful
[/quote]
Good point. But I wouldn't use it while developing a piece of code (when we all need all the help we can get back from error reporting and warnings).
Legend has it that reading the manual never killed anyone.
My site

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 June 2006 - 11:59 AM

[!--quoteo(post=380562:date=Jun 6 2006, 07:56 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 6 2006, 07:56 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Good point. But I wouldn't use it while developing a piece of code (when we all need all the help we can get back from error reporting and warnings).
[/quote]


That's true.

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 June 2006 - 12:00 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]But it also hides warnings.[/quote]
That's what AndyB said. I would recommend you do not use it while debugging your code.

I use it only in situations where I know I will get an error that I want to ignore. For example, if I use the mail fumction -- when I'm write and testing on my laptop, I know the mail function will give me an error because I haven't set it up in the PHP.INI file for the PC. So I don't want to see the error everytime -- yes I can wrap the code in an "if" statement to test whether I'm on the PC, but I took the "easy" way out. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Ken

#7 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 06 June 2006 - 12:32 PM

I agree in saying that you should avoid supressing errors like this.

when developing, i would enable all error messages, even warnings and notices.

when publishing, it might be best to hide errors via error_reporting() and using an error handler to quitely inform you of the errors and displaying a custom predefined error message to the users of the web app/site.

and like kenrbnsn said, i would only use @ if absolutely necessary.



here's an example for using @ when including files:

<?php
    if(@!include 'config.php')
    {
        echo 'A required configuration file could not be included. Script terminating.';
        exit(0);
    }
?>




IMO, when publishing the system, if you're handling the problem yourself, it's ok to hide them from everyone else...so long as person in charge (YOU) knows what's going on.

#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 June 2006 - 01:07 PM

[!--quoteo(post=380571:date=Jun 6 2006, 08:32 AM:name=Bane)--][div class=\'quotetop\']QUOTE(Bane @ Jun 6 2006, 08:32 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
IMO, when publishing the system, if you're handling the problem yourself, it's ok to hide them from everyone else...so long as person in charge (YOU) knows what's going on.
[/quote]

to me, that's definitely the key. your code should be solid enough that you could leave the "@" off of every function in your application and never throw any warnings or errors (hypothetically). however, what happens when you have some portion of your app that allows for user input? there is no guard against user error, so in those cases, we've got to follow what bane suggests: account for the errors yourself. for instance, i have written a couple classes for a blog that allow many different types of user input, but if something is input improperly, an error may be thrown. in this case, rather than having the ugly PHP error thrown across the screen, i'd much rather catch the error myself and display it in a useful format. as such, i will use the @ symbol to suppress errors and run a check like bane shows above.

however, as i mentioned above, coding your application to run smoothly with error reporting set to "E_ALL" is a great practice anyway.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#9 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 06 June 2006 - 01:56 PM

I tend to use '@' when some error may reveal sensitive information (like when I use flatfile databases, I always use @).

The bad thing is, it cannot be overridden by error_reporting(E_ALL), so I guess I'd be a better idea just to turn off error reporting...
~ D Kuang

#10 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 06 June 2006 - 05:31 PM

you can also use [a href=\"http://www.php.net/set_error_handler\" target=\"_blank\"]set_error_handler()[/a]. actually this is how it should be done. we shouldnt allow PHP to display its errors, we should be able to read PHP's message and display our own message to the viewer....what if your viewer doesn't speak english? If your system is not language dependant, you can use set_error_handler() and customize your message in their language.

oh and i'm quoting this from the above link (it's in the "Description"):
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
It is important to remember that the standard PHP error handler is completely bypassed. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.
[/quote]



Another useful link in the PHP manual for the @ error operator:
[a href=\"http://www.php.net/manual/en/language.operators.errorcontrol.php\" target=\"_blank\"]Error Control Operators[/a]


i hope this dissuades you from using the @ operator where not necessary in the future :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users