Reaver
Reaver

Reputation: 1

Globalize variables in php

I have 2 files. Lets say :

first.php

$a = 'blah';
echo 'echo2='.$a;

function foo(){
global $a;
echo 'echo3='.$a;
return $a;
}

second.php

require_once(path/to/the/file/first.php);
echo 'echo='.$a;
$b = foo();
echo 'echo4='.$b;

running the second.php file I get the following output :

echo=blah
echo2=blah
echo3=
echo4=

My question is "why I can't access variable $a in the function foo !

Upvotes: 0

Views: 944

Answers (2)

TigerTiger
TigerTiger

Reputation: 10806

or use

 $GLOBALS["Your_var_without_dollar_sign"];

http://php.net/manual/en/reserved.variables.globals.php

Upvotes: 1

Brad
Brad

Reputation: 163232

Change $global to global. That should fix it.

http://php.net/manual/en/language.variables.scope.php

Upvotes: 3

Related Questions