Sometimes when golfing in PHP, one uses tricks as reading/pushing to an inexistent variable, or using deprecated functions such as split()
, but those things outputs Warnings and Notices, which by themselves are not really errors and don't change how the code works.
Is there any policy regarding the output of these type of errors?
Should I add provisions to remove or hide the warnings in my code even if the questions doesn't ask it as a requirement or should I consider that the PHP configuration has error_reporting = 0
?
I have seen C answers that output compiler warnings (the closest equivalent I can think now)
2 Answers 2
Assume Default Settings
I have been informed with some frequency that my PHP solutions "don't work", typically due to an abundance of E_NOTICE
messages. I suggest the following settings be assumed by default:
short_open_tag = On
precision = 14
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
These are all default values, as they would be without a configuration file. For example, the following should be valid, even in challenges where producing any errors is prohibited:
<?for(;$i++<10;);
despite generating an E_NOTICE
, due to accessing a previously undefined variable. This message is by default not displayed. On a similar note, suppressing errors with @
should also be allowed in these cases.
-
1\$\begingroup\$ i know it's not competitive, that's why i try to use it anyways. i don't have to imagine the mysql error in the wild, i have seen it too often already \$\endgroup\$Einacio– Einacio2014年06月02日 14:10:59 +00:00Commented Jun 2, 2014 at 14:10
-
\$\begingroup\$ Too lazy to search for it now, but I read that
-n
(no php.ini) comes free. \$\endgroup\$Titus– Titus2016年12月21日 18:09:13 +00:00Commented Dec 21, 2016 at 18:09 -
\$\begingroup\$ @Titus I couldn't find it either, but this post likely predates it. I wouldn't say that
-n
is "free", but rather implicit. \$\endgroup\$primo– primo2016年12月22日 02:19:02 +00:00Commented Dec 22, 2016 at 2:19
There currently is no policy about that, but this is my opinion:
- Does the question explicitly disallow code that can lead to a Warning/Notice? Then it is disallowed.
- Does the question say nothing about such code? Then it would say it is allowed (and you can assume that
error_reporting = 0
), because none of the rules say that it is disallowed.
-
\$\begingroup\$ the question has been in my mind for a while, but i posted now after this question:codegolf.stackexchange.com/questions/28801/missing-odd-numbers in which that rule was added after i posted my solution \$\endgroup\$Einacio– Einacio2014年05月30日 16:38:17 +00:00Commented May 30, 2014 at 16:38
-
\$\begingroup\$ @Einacio: A rule update after an answer is posted is discouraged, because it can invalidate answers, and it invalidated yours. I'll remove that rule. \$\endgroup\$user3094403– user30944032014年05月30日 16:52:43 +00:00Commented May 30, 2014 at 16:52
-
\$\begingroup\$ actually, i had to add 1 character to avoid a warning. the decrease was beacuse a reorganization of code (which didn't output errors). anyways, this question is to see if we can have some official policy regarding this, i don't care about the specifics of that answer \$\endgroup\$Einacio– Einacio2014年05月30日 16:57:56 +00:00Commented May 30, 2014 at 16:57
-
\$\begingroup\$ @Einacio: I have removed that rule now and I leaved a comment. \$\endgroup\$user3094403– user30944032014年05月30日 16:58:23 +00:00Commented May 30, 2014 at 16:58
php ./program.php 2>/dev/null
"? \$\endgroup\$display_errors
is set to'stderr'
\$\endgroup\$