0

This is a newbie question, and I know it.

Template structure is your usual index.php, with a few require_once()'s for the header/footer etc.

I define a var at the top of index.php before any of the require_once()'s for the base url, such as $url = 'http://url';

I then want to echo this out into all template files, header/index/footer etc, it works inside index.php as expected, but fails with a undefined var in all template files that are included in.

I know it's a var scope issue, but I'm totally perplexed how to fix it.

I'm aware that the manual says vars are available to included files, however they aren't. Could it be a issue with my local PHP install?

edit : Created a couple of test files, and a var is defined between 2 files, so why are they not working on my main site files?

Any helps gracefully recieved.

Many Thanks

4
  • You need to show us how you invoke the templates. If there's a helper function in between the $url= declaration and the include() statement, you enter a new scope. Commented Jun 22, 2012 at 11:18
  • I just simplified it down to the bare minimum with a basic include($var.'header.php') then attempted to echo the $var on line 1 in header.php and it's still coming back as undefined when run. Really odd. Commented Jun 22, 2012 at 11:21
  • Solution found here drupal.org/node/84410 Commented Jun 22, 2012 at 11:39
  • That means your description was a lie. You're not including a file, you're loading a remote resource. Commented Jun 22, 2012 at 11:49

1 Answer 1

1

if you use functions or methods (functions in classes) then you need to do global $variable inside the function. Otherwise you will not have access to it, you also could define it as constant. A constant is always global.

define('MYURL', $url);

You might want to use a PHP framework, if you not already do so.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.