Hi,

I have a problem with global variable

Let me explain:
I have a ressource file . that contains variables:
$ressource_1 ='toto';
$ressource_2 = 'titi';

I have a Display class that contains calls to the ressource file (with a require_once.) and to a function that deals with printing pages:

function displaypage()
global $ressource_1,
$ressource_2;

echo $ressource_1;
echo $ressource_2;

The problem is that variables are empty!!!

The very strange think is that this works with some files and not with some others.
(We had to reinstall the server and since, all the new pages don't work whereas the older ones still work)

Any idea of where the solution could be? Is there a lilitation on the number of global variable that can be used ?

Thanks for any help. this is really annoying.

    There must be some difference between the older pages that still work and the new pages that don't work. Software may be faulty but it's never capricious. Without knowing what the difference is, I'll offer these suggestions:

    Is the path to the included files specified somehow in one set of files, and not the other, or specified differently?

    When you include the file inside your class (in a function, presumably), do you then try to echo it in a different function? The variables in a file included inside a function, and the variables inside any function, are local to that function. Even "global" won't change that. Here's a test script to show what I mean:

    test.php

    $var = 'var'; 
    
    function define_func_var()  
    { $func_var = 'func_var'; include 'include_file.inc'; } function echo_var()
    { global $var; define_func_var(); global $func_var; global $include_var; echo '$var = ' . $var . '<br />'; // Will be echoed echo '$func_var = ' . $func_var . '<br />'; // Won't be echoed echo '$include_var = ' . $include_var . '<br />'; // Won't be echoed } class Test { function include_file() { include 'include_file.inc'; } function test_echo_var() { global $var; define_func_var(); global $func_var; global $include_var; echo '$var = ' . $var . '<br />'; // Will be echoed echo '$func_var = ' . $func_var . '<br />'; // Won't be echoed echo '$include_var = ' . $include_var . '<br />'; // Won't be echoed } } echo 'Stand-alone function:' . '<br />'; echo_var(); echo 'Class function:' . '<br />'; $test = new Test; $test->include_file(); $test->test_echo_var();

    include_file.inc

    $include_var = 'include_var';

    Edit: Improved example script. The outcome is the same.

      Is the path to the included files specified somehow in one set of files, and not the other, or specified differently?
      The specification is the same and we know that the file is loaded ( an echo in the file shows the right variable)

      When you include the file inside your class (in a function, presumably), do you then try to echo it in a different function?
      No !

      It seems that other people have experienced the same bug and it is referenced in the php bug database of php.net.

        I guess that solves that. One of these days I'll do a search for this bug report (being sure to cover all the possible versions of PHP you may be using under all the possible server configs, etc.), and when I find it, then I'll know what it is, and I'll be able to provide a link to it to help others.

        Glad to hear you''re not experiencing your annoying problem anymore.

          Write a Reply...