The Joel on Software Discussion Group (CLOSED)

A place to discuss Joel on Software. Now closed.

This community works best when people use their real names. Please register for a free account.

Other Groups:
Joel on Software
Business of Software
Design of Software (CLOSED)
.NET Questions (CLOSED)
Fog Creek Copilot

The Old Forum

Your hosts:
Albert D. Kallal
Li-Fan Chen
Stephen Jones

what does @ operator do in php?

I've tried to look up what the @ operator does in PHP using the online help manual at

but failed.

What does @ do in PHP?

The line of code in question is:

$mail_sent = @mail( $to, $subject, $message, $headers );
Monday, February 25, 2008
ptr2void Send private email
Monday, February 25, 2008
it makes it so the function won't display error messages.
Don Dickinson Send private email
Monday, February 25, 2008
Warning are suppressed. It's like silent fail.
Victor Noagbodji Send private email
Monday, February 25, 2008
Generally you'll use that when you want to handle any errors yourself rather than having PHP output the error message to the client and possibly abort. One thing to watch out for, especially when porting code written for a newer PHP: If you call a nonexistent function with @, your script will abort with no error message.
Monday, February 25, 2008
@ is the bad coder's magic cure

try/catch is the good coder's tool

Monday, February 25, 2008
try..catch is wrong tool if you want to completely ignore an error.  @ is so much simpler and more straight forward.  It's a very rare thing to use but occasionally it makes sense.
Almost H. Anonymous Send private email
Monday, February 25, 2008
@ doesn't "ignore" an error - it prevents an error message being written out to the web page you're producing.

The error still occurs, and must still be handled (or can still be ignored) in exactly the same way as without the @ prefix.
Nit Picker
Monday, February 25, 2008
A valid use in my book is to replace this:

if ( IsSet ( $_GET['SomeVar'] ) && Trim ( $_GET['SomeVar'] ) != '' )
{ ... }

with this:

if ( Trim ( @$_GET['SomeVar'] ) != '' )
{ ... }

in other words: use it when you absolutely know what you are doing and know the results (eg: don't throw @ in your code just because you don't know how to fix a error message :).
Jan Hančič Send private email
Tuesday, February 26, 2008

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz