Bug #68105 [Wfx]: Switch shouldn't show notices for undefined variables
| From: | requinix@php.net | Date: | Sat, 27 Sep 2014 01:14:31 +0000 |
| Subject: | Bug #68105 [Wfx]: Switch shouldn't show notices for undefined variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187728@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68105&edit=1
ID: 68105
Updated by: requinix@php.net
Reported by: jacob87 at o2 dot pl
Summary: Switch shouldn't show notices for undefined
variables
Status: Wont fix
Type: Bug
Package: Output Control
PHP Version: 5.5.17
Block user comment: N
Private report: N
New Comment:
Like the default value for a function? You mean
function example($foo = 123) { }
example($bar);
Because that will raise a warning too. Besides, $foo will be null, not the default value of 123.
1. switch's default handles when the value you use was not listed in one of the cases. There
still has to be a value. If you try to specify that value with a variable that doesn't exist
then PHP should warn you about that like it does everywhere else.
2. switch is a glorified if/else tree. Putting aside that there is no if/else equivalent to a switch
with only a default, which is fine because it would be utterly pointless to use anyway, the if
equivalent to a switch is of the form
if ($action == "value 1") { ... }
else if ($action == "value 2") { ... }
else { ... }
That will also throw warnings because, despite there being a "default" at the end, PHP
still needed to know the value of $action for the rest of the conditions.
The more technical and precise equivalent is actually
$temp = $action;
if ($temp == "value 1") { ... }
else if ($temp == "value 2") { ... } // etc
else { ... }
where $temp is used to avoid re-evaluating the expression (eg, function calls), but that really just
cuts the number of warnings down to one on that first line.
3. It would be horribly inconsistent if PHP did not warn in this one special circumstance. So either
we make the warnings less frequent, where the only consistent solution is to remove them entirely
(or else you're stuck with remembering where the warnings are used and not used), or more
frequent, where we don't do this thing.
4. What you seem to be missing is that the warnings are good. This is the most important point.
Those warnings indicate a problem in your code: there is a logical error if you do not allow a value
to be set but then try to test that value later on. That should be fixed instead of altering the
language to make the warnings easier to cover up.
5. It is trivial for you to avoid the warning:
a) The best course of action is to not test an undefined variable. This could be as simple as
setting a default value at the start of the code, or you can move your switch around so that it only
executes when you know the variable was defined.
b) Use isset(), even though you said it was "redundant". Speaking of, I don't see how
it's redundant because it's taking an action that you hadn't taken yourself.
c) Error suppression operator. It's almost always a Bad Idea to use it, but it is available.
And finally, if your problem is actually output before headers then use error_log (or custom error
handling routines) to write to a file. You should still fix the warnings raised, of course, but at
least your code will work until you do.
Previous Comments:
------------------------------------------------------------------------
[2014-09-26 23:03:28] jacob87 at o2 dot pl
What kind of harm whould that possibly do?!
If switch has default value, it should be the same as default value for function!
Please, consider this once again.
------------------------------------------------------------------------
[2014-09-26 09:17:07] requinix@php.net
Doing so would cause much harm for very little good. The variable is undefined so don't try to
use it, period. It's a simple rule that's very easy to follow.
------------------------------------------------------------------------
[2014-09-26 08:25:45] jacob87 at o2 dot pl
Description:
------------
Switch should not show notices about undefined variables, as it can have it's default action.
So, the notice shouldn't show up if switch has default action (best implementation).
Or not show notices at all (easier).
You could say "just turn off notices in production environment". That is not the case. In
development environment some scripts can fail because of "Headers already sent". And I
don't want to write redundant code such as if(!isset($var))$var='';
Test script:
---------------
switch($action){
default:
echo 'This shouldn't show a notice!';
break;
}
Expected result:
----------------
This shouldn't show a notice!
Actual result:
--------------
Notice: Undefined variable: action in (...)
This shouldn't show a notice!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68105&edit=1