Using @ as error suppressor?

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

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Using @ as error suppressor?

Post by ben.artiss »

Hi,

I came across a way of sending emails which suppresses php errors if the mail server isn't set up:
$mail_sent = @mail($to,$subject,$msg,$headers);

- and I just wanted to confirm that you could do this with echo. On the login page of this site I'm working on, if the user enters the wrong details then the value of the username input is set to what the user entered (using the $_SESSION array). I have tried using:
<input type="text" name="username" value="<?php @echo $_SESSION['username']; ?>" />

but that caused a parse error, but it seems to work fine using 'echo @$_SESSION['username']'. Is this right? And does it work on all platforms/php versions? Also.. if I understand it correctly, could you use this to avoid errors with any undefined variable? I.e. echo @$_COOKIE['eatme'].@$_GET['you'] ..?

Thanks in advance!
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Using @ as error suppressor?

Post by mickeyunderscore »

@ suppresses errors from functions (I think), echo isn't a function.

But you shouldn't really be using @ so freely, it's better to error check rather than suppress.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Using @ as error suppressor?

Post by ben.artiss »

Great stuff thanks for the advice - it looks like I haven't been taking the 'long route'! :) One last thing though, just because it seems to work, does the @ work on both functions and variables? I only ask because it works as I want it to when I use 'echo @$_SESSION['username']'.
rvlira
Forum Newbie
Posts: 2
Joined: Sat Feb 07, 2009 7:50 am

Re: Using @ as error suppressor?

Post by rvlira »

According to PHP manual, the @ operator can be used before any expression (not only functions).

http://php.net/manual/en/language.opera ... ontrol.php

I also found a site with some interesting examples:

http://thesmithfam.org/blog/2006/05/07/ ... -operator/

Regards,
Post Reply