1

I have a .php script with 2 variables. $company and $user. During this .php script I require_once "/var/www/etc/etc/etc/etc/"

The .php file that I require_once, the variables from the previous .php script dont' transfer over. I can't figure out why, or what I am doing wrong. Ex.

master.php script

$company = "Some Company";

$user = "John";

require_once "/var/www/$company/$user/example.php

example.php script

$myFile = "/var/www/$company/$user/Template/Download/example.php";

The data that is held in the variables $company & $user doesn't hold through on the example script. I can't understand why.

Thanks

Galen
  • 29,976
  • 9
  • 71
  • 89
John
  • 131
  • 1
  • 1
  • 3
  • 1
    Are you sure that the script isn't erroring because you forgot `";` in your require_once call? – Galen May 02 '11 at 18:45
  • Nope that was just a typo here. It does have the ; on my code – John May 02 '11 at 18:47
  • Is there an error being thrown? If not, try turning on the highest level of [error reporting](http://www.php.net/manual/en/function.error-reporting.php) (include the line `error_reporting(E_ALL);` at the top of your script) and provide the errors that are shown. – eykanal May 02 '11 at 18:49

3 Answers3

5

If variables are used outside their scope, you need to use the keyword "global":

$foo = 'bar';

function fooBarBad() {
    echo $foo; //will echo nothing
}

function fooBarOk() {
    global $foo;

    echo $foo; //will echo bar
}
AlexV
  • 22,658
  • 18
  • 85
  • 122
0

Are you using the variables inside a function in the new page? If this is the case then you need to register them as global variables inside the function. Otherwise the variables should be read ok in the script calling them from the include.

robert_murray
  • 2,070
  • 17
  • 9
  • Yes I am using the variables inside a function on the new php page. How do I register them as global inside the function? And do I register them as global inside the function, or before? – John May 02 '11 at 18:51
0

Are your variables inside a function? if this is the case you probably lost your variables.

In this case, you would have to use either the "global" keyword inside the function, or use the $GLOBALS variable to register your variables.

To really see if your variables passed correctly, you can do

var_dump($company);
var_dump($user);

at the top of the file example.php

jsgoupil
  • 3,788
  • 3
  • 38
  • 53