Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts

    why doesnt this variable become global

    ok iv set up a simple demo

    this is the code im running

    PHP Code:
    include('simple_html_dom.php');
    include(
    'config.php');
    include(
    'connect.php');
    include(
    'functions.php');
    include(
    $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR 'globalFunctions.php');

    test();
        
        echo 
    "test variable = ".$test;
        
        exit; 
    this is the function which is in globalFunctions.php which is two directories back ../../

    PHP Code:
        function test() {
        
    $test 10;
        
        global 
    $test;
        } 
    how come when i run script i am not getting variable echoed
    Last edited by kevinkhan; 09-14-2011 at 09:41 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Because you cannot promote a variable to a global, you can only load one. $test as defined in main() would be a global one (that is, anywhere in the scope of the main running script; PHP's dummy main is not declarable as such). If $test does not reside in the symbols table, it will create it with a null value.
    PHP Code:

    function test()
    {
        
    $test 10;
        
    // test::$test is declared with a value of 10.
        
    global $test;
        
    // test::$test is declared with a value of main::&$test  
    }

    test();
    print 
    $test// void. 
    global (which you should not ever use except in a situation where a callback cannot accept a modified function signature), must be declared BEFORE the local function variable in order to change. The example above loads the $test no problem, but since its already void it overwrites the local $test with the reference of the main's $test.
    Last edited by Fou-Lu; 09-14-2011 at 11:30 PM.

  • Users who have thanked Fou-Lu for this post:

    kevinkhan (09-15-2011)

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Because you cannot promote a variable to a global, you can only load one. $test as defined in main() would be a global one (that is, anywhere in the scope of the main running script; PHP's dummy main is not declarable as such). If $test does not reside in the symbols table, it will create it with a null value.
    PHP Code:

    function test()
    {
        
    $test 10;
        
    // test::$test is declared with a value of 10.
        
    global $test;
        
    // test::$test is declared with a value of main::&$test  
    }

    test();
    print 
    $test// void. 
    global (which you should not ever use except in a situation where a callback cannot accept a modified function signature), must be declared BEFORE the local function variable in order to change. The example above loads the $test no problem, but since its already void it overwrites the local $test with the reference of the main's $test.

    Ok Thanks for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •