moving companies : Computers : Programming : Languages : PHP : kms : General

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

24 of 26 people (92%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I create variables that are available in all scripts?
How can I create an array to access from various scripts without passing it using GET or POST?

Jan 22nd, 2000 08:34
Nathan Wallace, Matt Gregory, Dave Martindale,


Use the require function:
Create a globals.php3 script, put your array inside:
  <?php
    //script globals.php3
    $myarray[] = "Item1";
    $myarray[] = "Item2";
    $myarray[] = "Item3";
  ?>
then require the script in any scripts which need the array:
  <?php
    //script usearray.php3
    require("globals.php3");
    print($myarray[0]);
  ?>
You can use the auto_prepend_file directive to automatically include the
globals.php3 file before all scripts on a site.
Remember that if you want to use the declared variables inside a
function you will need to define them as being global in that function. 
For example:
    <?php
    function test () {
        global $myarray;
        // do stuff
    }
    ?>