In PHP, what is the significance of '@' as a prefix to the variable?

This is a discussion on In PHP, what is the significance of '@' as a prefix to the variable? within the PHP Language forums, part of the PHP Programming Forums category; In PHP Whats the difference between $yourname = @$_POST['yourname']; AND $yourname = $_POST['yourname']; In particular, what is the significance of '@' ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-21-2004
Anonymous
 
Posts: n/a
Default In PHP, what is the significance of '@' as a prefix to the variable?

In PHP
Whats the difference between
$yourname = @$_POST['yourname']; AND
$yourname = $_POST['yourname'];

In particular, what is the significance of '@' as a prefix to the variable?

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 10-21-2004
Chris Hope
 
Posts: n/a
Default Re: In PHP, what is the significance of '@' as a prefix to the variable?

Anonymous wrote:

> In PHP
> Whats the difference between
> $yourname = @$_POST['yourname']; AND
> $yourname = $_POST['yourname'];
>
> In particular, what is the significance of '@' as a prefix to the
> variable?


It suppresses any warning messages.

For example if you did this:

error_reporting(15);
print $foo;

you'd get an error along the lines of "Notice: Undefined variable: foo
in /home/chris/websites/toolboxa/test/run.php(48)" because the error
reporting level is set to show warnings in this example.

If you did

error_reporting(15);
print @$foo;

you won't get an error message / warning.

I hadn't seen it used for prefixing variables before but it is commonly used
when calling functions to suppress the error messages they disply eg:

$fp = @fopen('foo.txt', 'r');
if($fp) {
/* the file is open so do something here */
}
else {
/* the file couldn't be opened so do something else here */
}

Without the @ you'd end up with an error along the lines of "Warning:
fopen(foo.txt): failed to open stream: No such file or directory
in /home/chris/websites/toolboxa/test/run.php(48)" which you may not want
output to your webpage, console etc when you can deal with it in code.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Reply With Quote
  #3 (permalink)  
Old 10-21-2004
Michael Fesser
 
Posts: n/a
Default Re: In PHP, what is the significance of '@' as a prefix to the variable?

.oO(Chris Hope)

>If you did
>
> error_reporting(15);
> print @$foo;
>
>you won't get an error message / warning.


@ should only be used for functions if you handle errors yourself. Using
it to avoid notices about undefined variables/offsets is bad code. (IMHO
as bad as turning off notices completely on a development system.)

Micha
Reply With Quote
  #4 (permalink)  
Old 10-21-2004
Chris Hope
 
Posts: n/a
Default Re: In PHP, what is the significance of '@' as a prefix to the variable?

Michael Fesser wrote:

> .oO(Chris Hope)
>
>>If you did
>>
>> error_reporting(15);
>> print @$foo;
>>
>>you won't get an error message / warning.

>
> @ should only be used for functions if you handle errors yourself. Using
> it to avoid notices about undefined variables/offsets is bad code. (IMHO
> as bad as turning off notices completely on a development system.)


I completely agree with you. I think coding in development with notices ON
is essential. It helps you catch bugs more easily if they're related to a
variable not already being set when you first reference it.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:14 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0