Im having a tough time accessing variables from my own external file.

I gave this file:

?php include_once('/exercise_names.php'); ?

Which contains the following:


//Exercise Names

$memory01 = '1'; 
$memory02 = '2';
$memory03 = '3';
$memory04 = '4';


I cannot get the page to render the names. I keep getting the following errors:

Notice: Undefined variable: memory01 in eval() (line 4 of /home/webapps/drupal/releases/modules/php/php.module(80) : eval()'d code).

Basically its telling me it cant find the required variables, but they are in the file. For some reason it cannot read the included file.

Any ideas?

thanks, Mac

  • 1
    Your include statement needs to be in a module file so that it's included in every page load, including the one thats running the eval code. You could put in your themes template.php file I guess, but a custom module is the correct place as @MPD has already mentioned – Clive Apr 7 '12 at 12:57

If I have a module called foo, I put all of my includes in a subdirectory called 'inc/' and then use this


require_once(dirname(__FILE__) . "/inc/constants.inc");
require_once(dirname(__FILE__) . "/inc/class.FOO.inc");
require_once(dirname(__FILE__) . "/inc/class.BAR.inc");

in foo.module. If doing Drupal 7, you may also want to take a look at the files directive for .info files:

Drupal now supports a dynamic-loading code registry. To support it, all modules must now declare any code files containing class or interface declarations in the .info file, like so:

files[] = example.test

When a module is enabled, Drupal will rescan all declared files and index all the classes and interfaces that it finds. Classes will be loaded automatically by PHP when they are first accessed.


Even if you aren't doing this in a module, you should really abstract out the constants into a module that provides getter functions and/or a static class and/or a singleton with getter functions.

Another option is to bail on includes, and use variable_set / variable_get, preferably in conjunction with a module settings form.

| improve this answer | |
  • This isn't a module, just a file of config variable names that I wan to call periodically. Basically I have a name for an device that I want to use all over the site. I want to put the names in a file and access them whenever. If i change the device name it will change it in every location. – Mac Apr 6 '12 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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