开发者

Variables not holding

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


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
}


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜