1

I have a bunch of database addresses and other random long strings that I must input into functions again and again all over my site. Is there a way to make a ton of permanent, global variables in PHP that will be valid on any file on the current PHP installation?

Or should I just make a vars.php with all of the variables defined, and always include that file?

1
  • You probably mean global constants. Commented May 3, 2011 at 23:07

3 Answers 3

5

Create your file with the settings you need. Something like this:

<?php
    // My file name might be config.php
    $config = array(
        'db.connection' => 'localhost:3306'
        // etc...
    );
?>

Then include it into each page/file that needs it.

<?php
    require("path-to/config.php");

    // Other stuff this file does/needs
?>
Sign up to request clarification or add additional context in comments.

4 Comments

i'd use require_once("file.php") :)
+1 except I agree with mister koz, you should use require_once
@mister, @andrew: I don't require_once unless I'm importing a class. The overhead is unnecessary given this instance because the values are, what should be, unalterable config values. Importing agian will not hurt anything.
Unless it is a file of constants, classes, functions, or anything else that kills when declared multiple times.
0

The answer is, well, both! Use prepend.

http://www.electrictoolbox.com/php-automatically-append-prepend/

4 Comments

If at all possible, it would be best to summarize the technique in some code within your answer.
Ick, please stay away form INI settings like this. Just use require or include on each file that needs it.
I see no reason to stay away from auto_prepend_file. It's used often for sessions or the exact purpose that the OP asked for. I don't personally use it, but I can't see a reason why using require() or include() repeatedly is better practice. BTW, OP, auto_prepend_file calls things like require() and not include() which means the script will crash if the file cannot be found.
because it is explict and easy to find/read from the code. Using INI settings means that it would be necessary to read/find server configuration in addition to code.
0

Well you could use $GLOBALS variable, which also contains other superglobals like $_GET, $_POST etc. Of course that means you should still always include file where you define your own custom variable.

The good thing when using $GLOBALS is you can use it in all scopes, and at least imho you can use it as a sort of a namespacing technique.

For example if you use

$settings = array('host' => 'localhost');
    function connect() {
        global $settings;
    }

there's a possibility that you unintentionally override $setting variable at some point. When using $GLOBALS, I think this kind of scenario is not so likely to happen.

$GLOBALS['_SETTINGS'] = array(... => ...);
    function connect() {
       $settings = $GLOBALS['_SETTINGS'];
    }

Another solution would be using your own custom class, where you store your settings in a static array. Again, this requiers including file, where you define the class, but your data will be avialable within all scopes and cannot be acidently overriden.

Example:

class MySettings {

    private static $settings = array(
        'host' => 'localhost',
        'user' => 'root'
    );

    public static get($key) {
        return self::$settings[$key];
    }

}

function connect() {
    $host = MySettings::get('host');
    ...
}

Not saying it's the best way for doing this, but surely one way for accomplishing your goal.

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.