php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68105 Switch shouldn't show notices for undefined variables
Submitted: 2014-09-26 08:25 UTC Modified: 2014-09-27 01:14 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jacob87 at o2 dot pl Assigned:
Status: Wont fix Package: Output Control
PHP Version: 5.5.17 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-09-26 08:25 UTC] 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!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-09-26 09:17 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2014-09-26 09:17 UTC] 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 23:03 UTC] 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-27 01:14 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC