My wordpress theme consists of the file loop.php which just does this: <?php include("AAAA.php"); ?>. Now AAAA.php looks like this:

  $test = 1;
  function f() {
    global $test;
    echo "inside f: test='$test'";
  echo "outside f: test='$test' ";

Since I referred to $test inside f, I would suspect that f knows thereafter about the value of $test but actually I obtain this:

outside f: test='1' inside f: TEST=''

I suspect that I am making a very trivial error... Can somebody point it out to me?

Regards, FW.

  • 4
    This works only if you have included loop.php in a global context. Avoid global variables and constants like hell, they are really, really bad code.
    – fuxia
    Mar 25, 2015 at 16:17
  • 2
    Globals work in WordPress just as in any other PHP, though as toscho states they can be a pain and should be avoided when at all possible. Maybe some of this will help: wordpress.stackexchange.com/search?q=user%3A21376+global
    – s_ha_dum
    Mar 25, 2015 at 16:36

1 Answer 1


Don't use globals, but if you must use them you should explicitly declare them as such. Wordpress includes theme files from functions therefor a $test = 1; will be evaluated in the context of the function and the variable $test will be implicitly declared in the context of the function but not as a global.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.