php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2193 Global declared variables not gobal
Submitted: 1999-08-30 10:25 UTC Modified: 1999-08-30 11:08 UTC
From: lemming at k332 dot feld dot cvut dot cz Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0 Beta 2 OS: Linux 2.0.36
Private report: No CVE-ID: None
 [1999-08-30 10:25 UTC] lemming at k332 dot feld dot cvut dot cz
It may be just "feature", but it is not documented to behave so:

If I include file within a function and that file declares global
variables, they are not really global:

file t1.php:
<?
  $T=1;
function Test()
{
  global $T;
  echo "T=$T\n";
}

  Test()
?>

file t2.php:
<?
function Test2()
{
  include('t1.php');
}

  Test2();
?>


'php t1.php' gives
T=1

'php t2.php' gives
T=
it is (unset).

If I define T global in Test2(), it works - but it requires
Test2 to know which global variables (configuration
etc...) Test() uses...


Michal Kara alias lemming

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-08-30 11:08 UTC] zeev at cvs dot php dot net
Your code is wrong.
When you include a file from within a function, the included
code is executed in the scope of that function.  Thus, when you
include t1.php from Test2(), $T=1 is executed in the scope of
Test2(), and does not become a global variable.  For that reason,
global $T inside Test() does not attach to this variable.

 [2002-02-19 05:34 UTC] hvalette at free dot fr
Hi,

I have the salme problem.
I'm not a PHP specialist and i can't find any solution.

Have you found a solution for it ?
Could you explain me the fix ?

Thank you in advance.
HV
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Sep 20 08:01:26 2020 UTC