toplisek
12-19-2005, 08:36 AM
I have seen e.g. code <?php echo @$_GET['index']; ?>
what actually means @ in such cases?
what actually means @ in such cases?
|
Click to See Complete Forum and Search --> : Character @ in PHP code toplisek 12-19-2005, 08:36 AM I have seen e.g. code <?php echo @$_GET['index']; ?> what actually means @ in such cases? chazzy 12-19-2005, 09:11 AM 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. toplisek 12-19-2005, 09:26 AM 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. bokeh 12-19-2005, 09:35 AM in that case, it does nothing and shouldn't be used in that way.Rubbish! In this instance it is being used to suppress an E_ALL level notice. Instead of writing out <?php echo (!empty($_GET['index'])) ? $_GET['index'] : null; ?> although most experts think it should not be used. ShrineDesigns 12-19-2005, 09:40 AM the @ mutes or suppresses any errors that may occur, and don't neccessarily have to be used with a function, example<?php error_reporting(E_ALL); @($str .= 'foo'); // $str is not defined and would result in an error $foo .= 'bar'; // outputs an error ?> EDIT: bokeh good example :) bokeh 12-19-2005, 09:41 AM Duplicated posting... bokeh 12-19-2005, 09:53 AM ...The following is from empty() (http://php.net/empty) no warning is generated when the variable is not set. ShrineDesigns 12-19-2005, 10:43 AM oops, sorry i was think of something else (multi-tasking and lack of sleep, a bad combo lol) chazzy 12-19-2005, 11:02 AM 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 mysql_select_db("database1"); 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 @mysql_select_db("database1"); 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. NogDog 12-19-2005, 11:34 AM @ 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.: $db = @mysql_select_db($database); if(!$db) { # output a custom error message here } else { # continue processing } bokeh 12-19-2005, 12:19 PM However, if you use @ to suppress error reporting, it now becomes incumbent upon you to do your own error-checking and -handlingI agree with that but this case is rather different. It is quite normal to make a form sticky like so: value="<?php echo $_GET['index']; ?>"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. Reli4nt 12-20-2005, 08:40 AM I agree with that but this case is rather different. It is quite normal to make a form sticky like so: 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. chazzy 12-20-2005, 09:53 AM I agree with that but this case is rather different. It is quite normal to make a form sticky like so: value="<?php echo $_GET['index']; ?>"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. 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"; SpectreReturns 12-20-2005, 01:38 PM 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. NogDog 12-20-2005, 01:41 PM The could 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. chazzy 12-20-2005, 01:43 PM 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. i just hate how this is valid: $string = "this is a string"; if($string == "this is a string"){ $string = TRUE; } else{ $string = FALSE; } if(!$string){ $string = 4; } else{ $string= 128; } 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... SpectreReturns 12-20-2005, 08:37 PM Pointers are useless in scripting languages (and by * I meant a pointer to an array). Reli4nt 12-20-2005, 08:43 PM 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. webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |