Results 1 to 2 of 2

Thread: Hi all,

  1. #1
    Frontpage User
    Join Date
    Dec 2006
    Posts
    20
    Post Thanks / Like

    Default Hi all,

    Hi all,

    I read in php manual:

    Global variables: $GLOBALS
    Note: $GLOBALS has been available since PHP 3.0.0.
    An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.
    This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $GLOBALS; to access it within functions or methods.
    So If You don't need to do a global $GLOBALS, how to deal with this $GLOBALS so that the variable exist in function.
    I made example below:

    Code:
    <?php
    $basic_url = 'http://www.php.net';
    $GLOBALS = ..........;
    function whatever() {
     echo $GLOBALS.....;
    }
    whatever();  // would print 'http://www.php.net'
    ?>
    How to deal with the $GLOBALS so the above script will produce 'http://www.php.net' without making a global $GLOBALS.

    Please advise..

  2. #2
    Wannabe Geek daviddoran's Avatar
    Join Date
    Aug 2006
    Location
    Meath, Ireland
    Posts
    342
    Post Thanks / Like

    Default

    This will work fine:
    Code:
    $basic_url = 'http://www.php.net/';
    function whatever(){
      echo $GLOBALS['basic_url'];
    }
    whatever();
    The reason this works is that PHP puts all variables (in the global scope) into the $GLOBALS array so you can access all variables in functions/classes etc.

Visitors found this page by searching for:

No visitors have come to this page from a search engine, yet!

Tags for this Thread

Posting Permissions

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