php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52426 switch() does not issue PHP Notice for uninitialized variable
Submitted: 2010-07-24 08:21 UTC Modified: 2010-09-20 14:59 UTC
From: gphemsley at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.14 OS:
Private report: No CVE-ID: None
 [2010-07-24 08:21 UTC] gphemsley at gmail dot com
Description:
------------
If the parameter passed to switch() is an uninitialized variable, a PHP Notice documenting such is not issued.

Test script:
---------------
<?php

error_reporting( E_ALL );

var_dump( PHP_VERSION, $mode );

switch( $mode )
{
    default:
        print '<p>There should be 2 notices of "undefined variable: mode".<br />' . "\n";
        print 'One for the var_dump() and one for the switch().</p>' . "\n";
    break;
}

highlight_file( $_SERVER['SCRIPT_FILENAME'] );

?> 

Expected result:
----------------
In the above script, two notices of "Undefined variable: mode" should be issued, one for the var_dump() and one for the switch().

Actual result:
--------------
In reality, only one notice is issued: the one for the var_dump(). (Taking out that call means that no notices are issued.)

I've gotten confirmation that this is present in 5.2.4, 5.2.5, and 5.3.2.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-18 12:17 UTC] brendel at krumedia dot de
If you add some cases to the switch the notice it triggered.
 [2010-09-20 14:37 UTC] iliaa@php.net
-Status: Open +Status: Bogus
 [2010-09-20 14:37 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Unless you have "case:" statements inside the switch the condition of the switch 
never actually gets used, because it does not get used it means no notices are 
being raised.
 [2010-09-20 14:59 UTC] gphemsley at gmail dot com
That seems counter-intuitive to me, to just ignore code like that. (Obviously, if I write "switch", I intend there to be a switch.)

I assume the portion of the documentation you're referring to (in your canned message) is this:
"In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements."

The context of those statements appears to be referring to the code within the switch, as is inherent in the use of a switch: If you don't hit a case that is true, then you don't execute the code in that case block. It says nothing about the contents of the switch() call itself.

If you insist that this is desired behavior (a claim which I disagree with), I'd at least recommend that the documentation be updated to make this clear.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 23:01:29 2024 UTC