tags:

views:

61

answers:

2

I have use

include ('base.php')

script in my other scripts ( more.php). Now base.php should receive one variable from more.php. How to pass a variable from more.php into base.php?

+6  A: 

If you include a script file A into a script file B, then A has the same global variable scope as B has:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope. — include manual page

So if you define a variable in A, then you have access to that variable in B:

// A.php
$varInA = 'foobar';
include 'B.php';

// B.php
echo $varInA;
Gumbo
A: 

Create a function within base.php, like so:

function setup($config) {
    // Do something with config
}

and call it from more.php..

setup($somevar);

Or, you could try using classes and member variables and constructors.

Josh Smeaton