0

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 !

2
  • 1
    There's also the syntax error with the require_once. If you don't give the exact code, you'll only get guesses, not answers. Specifically if the include occurs within a functions local variable scope, the $a= declaration won't be global. Commented Apr 27, 2012 at 17:20
  • the register_globals in php.ini is set to off. Thanks for your answers! Commented Apr 27, 2012 at 17:31

2 Answers 2

3

Change $global to global. That should fix it.

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

Sign up to request clarification or add additional context in comments.

Comments

1

or use

 $GLOBALS["Your_var_without_dollar_sign"]; 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.