/    Sign up×
Community /Pin to ProfileBookmark

Character @ in PHP code

I have seen e.g. code <?php echo @$_GET[‘index’]; ?>

what actually means @ in such cases?

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@chazzyDec 19.2005 — in that case, it does nothing and shouldn't be used in that way.

The @ operator, when used before a function supresses errors. it's smart to use for any production code, but harder to debug.
Copy linkTweet thisAlerts:
@toplisekauthorDec 19.2005 — what does it mean ,,supresses errors''. What does it mean that it will be ignored ? Need help

I have seen in manual: PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
Copy linkTweet thisAlerts:
@bokehDec 19.2005 — in that case, it does nothing and shouldn't be used in that way.[/QUOTE]Rubbish! In this instance it is being used to suppress an E_ALL level notice. Instead of writing out [code=php]<?php echo (!empty($_GET['index'])) ? $_GET['index'] : null; ?>[/code] although most [I]experts[/I] think it should not be used.
Copy linkTweet thisAlerts:
@ShrineDesignsDec 19.2005 — the @ mutes or suppresses any errors that may occur, and don't neccessarily have to be used with a function, example[code=php]<?php
error_reporting(E_ALL);
@($str .= 'foo'); // $str is not defined and would result in an error

$foo .= 'bar'; // outputs an error
?>[/code]


EDIT: bokeh good example ?
Copy linkTweet thisAlerts:
@bokehDec 19.2005 — [B]Duplicated posting...[/B]
Copy linkTweet thisAlerts:
@bokehDec 19.2005 — ...[/QUOTE]The following is from [URL=http://php.net/empty][B]empty()[/B][/URL]

[I] [CODE]no warning is generated when the variable is not set.[/CODE][/I]
Copy linkTweet thisAlerts:
@ShrineDesignsDec 19.2005 — oops, sorry

i was think of something else (multi-tasking and lack of sleep, a bad combo lol)
Copy linkTweet thisAlerts:
@chazzyDec 19.2005 — since no one really said it:

If you, let's say, select a database without first connecting to a server using the mysql api, with something like this
[code=php]
mysql_select_db("database1");
[/code]

it would give you an error along the lines of "unable to connect to MySQL server through /var/lib/mysql/mysql.sock (2)"

if you instead did
[code=php]
@mysql_select_db("database1");
[/code]

it wouldn't show that error and your script would continue to run. so basically if you don't use @ you will get errors when something breaks, but if you do use it, you won't know if you're getting errors right away.
Copy linkTweet thisAlerts:
@NogDogDec 19.2005 — @ can be useful to prevent error messages from showing up to the user, either due to security issues (you don't want any information showing up about how your script runs) or for cosmetic issues (you don't like the way PHP shows the errors). However, if you use @ to suppress error reporting, it now becomes incumbent upon you to do your own error-checking and -handling, e.g.:
[code=php]
$db = @mysql_select_db($database);
if(!$db)
{
# output a custom error message here
}
else
{
# continue processing
}
[/code]
Copy linkTweet thisAlerts:
@bokehDec 19.2005 — However, if you use @ to suppress error reporting, it now becomes incumbent upon you to do your own error-checking and -handling[/QUOTE]I agree with that but this case is rather different. It is quite normal to make a form sticky like so: [code=php]value="<?php echo $_GET['index']; ?>"[/code]which doesn't raise an error or a warning... but when error_reporting is set to E_ALL it raises a notice. Most people suppress that notice using isset() or empty() without further custom error handling so I don't really see a problem suppressing the error with @ and no custom handler in this instant.
Copy linkTweet thisAlerts:
@Reli4ntDec 20.2005 — I agree with that but this case is rather different. It is quite normal to make a form sticky like so:[/QUOTE]

Same here.

Under most circumstances using @ is dangerous especially in the development and beta stages, but in an instance such as this it is just one of many perfectly acceptable options. Arguably this is more concise than the alternatives.
Copy linkTweet thisAlerts:
@chazzyDec 20.2005 — I agree with that but this case is rather different. It is quite normal to make a form sticky like so: [code=php]value="<?php echo $_GET['index']; ?>"[/code]which doesn't raise an error or a warning... but when error_reporting is set to E_ALL it raises a notice. Most people suppress that notice using isset() or empty() without further custom error handling so I don't really see a problem suppressing the error with @ and no custom handler in this instant.[/QUOTE]

I guess it depends on how you think about it. Does the variable exist? in php, all variables seem to always exist (thus the reason why you don't need to give it a type, or anything like var $text = "hi there"; to declare it). maybe this is something we can look forward to in PHP6. String $text = "hi there";
Copy linkTweet thisAlerts:
@SpectreReturnsDec 20.2005 — I hope not. Typeless variables are much easier to program with (ever try C++ going back and forth from *string to *int[] without static_cast?), and there's no harm in them. Since PHP automatically converts everything to what it needs, changing this functionality would break _every_ PHP script written as of now.
Copy linkTweet thisAlerts:
@NogDogDec 20.2005 — The [i]could[/i] implement it such that only variables which you declare as a specific type are strongly typed, and otherwise by default they remain untyped. Then those who want to use a strongly typed language can, and those who don't won't have to.
Copy linkTweet thisAlerts:
@chazzyDec 20.2005 — I hope not. Typeless variables are much easier to program with (ever try C++ going back and forth from *string to *int[] without static_cast?), and there's no harm in them. Since PHP automatically converts everything to what it needs, changing this functionality would break _every_ PHP script written as of now.[/QUOTE]

i just hate how this is valid:
[code=php]
$string = "this is a string";
if($string == "this is a string"){
$string = TRUE;
}
else{
$string = FALSE;
}
if(!$string){
$string = 4;
}
else{
$string= 128;
}
[/code]


So it goes from type string to type boolean to type int, no explicit type casting etc.

I used to program C for a while. I honestly like pointers, then again I also liked Assembly so...
Copy linkTweet thisAlerts:
@SpectreReturnsDec 21.2005 — Pointers are useless in scripting languages (and by * I meant a pointer to an array).
Copy linkTweet thisAlerts:
@Reli4ntDec 21.2005 — it would offer a higher degree of security in some cases but the current flexibility makes php scripting so quick and pleasant. Almost casual vs. C programming.
×

Success!

Help @toplisek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...