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 '@' ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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/ |
|
|||
|
.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 |
|
|||
|
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/ |