#50042 [Bgs]: global variable not fully global when declared in a function within a function
| From: | adam dot jimenez at gmail dot com | Date: | Fri, 30 Oct 2009 19:25:29 +0000 |
| Subject: | #50042 [Bgs]: global variable not fully global when declared in a function within a function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-144116@lists.php.net to get a copy of this message | ||
ID: 50042
User updated by: adam dot jimenez at gmail dot com
Reported By: adam dot jimenez at gmail dot com
Status: Bogus
Bug Type: Variables related
Operating System: any
PHP Version: 5.3.0
New Comment:
//Global doesn't make a variable global but imports a variable fro
mthe
global scope (outside of all functions) into the current
function/method.
so how come it's added to the $GLOBALS array and can be accessed from
outside the function?
Previous Comments:
------------------------------------------------------------------------
[2009-10-30 15:30:39] johannes@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
Global doesn't make a variable global but imports a variable fro mthe
global scope (outside of all functions) into the current
function/method.
------------------------------------------------------------------------
[2009-10-30 15:03:46] adam dot jimenez at gmail dot com
Description:
------------
In the example function test2 is declared in function test.
Function test2 declares the variable $test as global.
$test is available inside test2 and outside the function but crucially
not in function test.
this may seem strange practice to declare a function within a function.
but it can be useful in a function or class-based template system which
is including other files and projects like wordpress.
see: http://wordpress.org/support/topic/288455
Reproduce code:
---------------
<?php
function test()
{
function test2()
{
global $test;
$test='nads'."\n";
}
test2();
print $test; //doesn't work
}
test();
print $test; //works
?>
Expected result:
----------------
nads
nads
Actual result:
--------------
nads
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50042&edit=1