Bug #52576 [Opn->Bgs]: Variable variables can create invalid variables.

From: Date: Tue, 10 Aug 2010 17:46:10 +0000
Subject: Bug #52576 [Opn->Bgs]: Variable variables can create invalid variables.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-153308@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52576&edit=1

 ID:                 52576
 Updated by:         johannes@php.net
 Reported by:        rquadling@php.net
 Summary:            Variable variables can create invalid variables.
-Status:             Open
+Status:             Bogus
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Windows XP SP3
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

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

Variable names may contain any character.

${',.-+#*#+\\'} = 42;

This is expected.


Previous Comments:
------------------------------------------------------------------------
[2010-08-10 18:21:13] rquadling@php.net

Description:
------------
It seems you can create a variable in $GLOBALS that is not accessible as
a 
normal variable.

I found this because I wanted to use variable variables to populate a
nested 
array.

Jim Lucas, in http://news.php.net/php.general/307392, comments
that the
use of 
eval() may be appropriate in the case.

That aside, the entry that is created in $GLOBALS doesn't follow the
rules for 
variable naming in
http://docs.php.net/manual/en/language.variables.basics.php,

namely that '[' and ']' are not valid variable names. I think this
should have t 
least produced an E_WARNING.

So, with all of that, I'm not sure if this is a bug or a documentation
problem.

I certainly think it is an edge case.

Regards,

Richard Quadling.

Test script:
---------------
<?php
// Show all errors.
error_reporting(-1);
ini_set('display_errors', 1);

// Expectation was to be able to create a deep array using variable
variables.
$a = 'b[1][2][3]';
$$a = 'Inaccessible?';
print_r($b);

// Hide superglobals
unset($_POST);
unset($_GET);
unset($_SESSION);
unset($_FILES);
unset($_COOKIE);
unset($_SERVER);
unset($argv);
unset($argc);

// Reveals inaccessible variable.
var_dump($GLOBALS);


Expected result:
----------------
Array
(
    [1] => Array
        (
            [2] => Array
                (
                    [3] => Inaccessible?
                )

        )

)
array(3) {
  ["GLOBALS"]=>
  *RECURSION*
  ["a"]=>
  string(10) "b[1][2][3]"
  ["b"]=>
  array(1) {
    [1]=>
    array(1) {
      [2]=>
      array(1) {
        [3]=>
        string(13) "Inaccessible?"
      }
    }
  }
}

Actual result:
--------------
Notice: Undefined variable: b in - on line 5
array(4) {
  ["GLOBALS"]=>
  *RECURSION*
  ["a"]=>
  string(10) "b[1][2][3]"
  ["b[1][2][3]"]=>
  string(13) "Inaccessible?"
  ["php_errormsg"]=>
  string(21) "Undefined variable: b"
}



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52576&edit=1


Thread (2 messages)

« previous php.bugs (#153308) next »