svec
svec

Reputation: 3487

How to setup site-wide variables in php?

I want to define something like this in php:

$EL = "\n<br />\n";

and then use that variable as an "endline" marker all over my site, like this:

echo "Blah blah blah{$EL}";

How do I define $EL once (in only 1 file), include it on every page on my site, and not have to reference it using the (strangely backwards) global $EL; statement in every page function?

Upvotes: 8

Views: 8044

Answers (11)

Ezekiel Arin
Ezekiel Arin

Reputation: 135

You might consider using a framework to achieve this. Better still you can use

Include 'functions.php';
require('functions');

Doing OOP is another alternative

Upvotes: 0

Progrock
Progrock

Reputation: 7485

You can use the auto_prepend_file directive to pre parse a file. Add the directive to your configuration, and point it to a file in your include path. In that file add your constants, global variables, functions or whatever you like.

So if your prepend file contains:

<?php
define('FOO', 'badger');

In another Php file you could access the constant:

echo 'this is my '. FOO;

Upvotes: 0

dbwebtek
dbwebtek

Reputation: 245

Do this define ('el','\n\<\br/>\n'); save it as el.php

then you can include any files you want to use, i.e

echo 'something'.el; // note I just add el at end of line or in front

Hope this help

NOTE please remove the '\' after < br since I had to put it in or it wont show br tag on the answer...

Upvotes: 2

Michał Niedźwiedzki
Michał Niedźwiedzki

Reputation: 12939

svec, use a PHP framework. Just any - there's plenty of them out there. This is the right way to do it. With framework you have single entry point for your application, so defining site-wide variables is easy and natural. Also you don't need to care about including header files nor checking if user is logged in on every page - decent framework will do it for you.

See:

Invest some time in learning one of them and it will pay back very soon.

Upvotes: 0

Darryl Hein
Darryl Hein

Reputation: 144987

Another option is to use an object with public static properties. I used to use $GLOBALS but most editors don't auto complete $GLOBALS. Also, un-instantiated classes are available everywhere (because you can instatiate everywhere without telling PHP you are going to use the class). Example:

<?php

class SITE {
    public static $el;
}

SITE::$el = "\n<br />\n";

function Test() {
    echo SITE::$el;
}

Test();

?>

This will output <br />

This is also easier to deal with than costants as you can put any type of value within the property (array, string, int, etc) whereas constants cannot contain arrays.

This was suggested to my by a user on the PhpEd forums.

Upvotes: 0

Brian Warshaw
Brian Warshaw

Reputation: 22984

Are you using PHP5? If you define the __autoload() function and use a class with some constants, you can call them where you need them. The only aggravating thing about this is that you have to type something a little longer, like

MyClass::MY_CONST

The benefit is that if you ever decide to change the way that you handle new lines, you only have to change it in one place.

Of course, a possible negative is that you're calling including an extra function (__autoload()), running that function (when you reference the class), which then loads another file (your class file). That might be more overhead than it's worth.

If I may offer a suggestion, it would be avoiding this sort of echoing that requires echoing tags (like <br />). If you could set up something a little more template-esque, you could handle the nl's without having to explicitly type them. So instead of

echo "Blah Blah Blah\n<br />\n";

try:

<?php
if($condition) {
?>
<p>Blah blah blah
<br />
</p>
<?php
}
?>

It just seems to me like calling up classes or including variables within functions as well as out is a lot of work that doesn't need to be done, and, if at all possible, those sorts of situations are best avoided.

Upvotes: 1

UnkwnTech
UnkwnTech

Reputation: 90861

@svec yes this will, you just have to include the file inside the function also. This is how most of my software works.

function myFunc()
 {
require 'config.php';
//Variables from config are available now.
 }

Upvotes: 0

TT.
TT.

Reputation: 1359

Sounds like the job of a constant. See the function define().

Upvotes: 3

UnkwnTech
UnkwnTech

Reputation: 90861

Sessions are going to be your best bet, if the data is user specific, else just use a conifg file. config.php:

<?php
$EL = "\n<br />\n";
?>

Then on each page add

require 'config.php'

the you will be able to access $EL on that page.

Upvotes: -1

Paige Ruten
Paige Ruten

Reputation: 176665

Most PHP sites should have a file (I call it a header) that you include on every single page of the site. If you put that first line of code in the header file, then include it like this on every page:

 include 'header.php';

you won't have to use the global keyword or anything, the second line of code you wrote should work.

Edit: Oh sorry, that won't work inside functions... now I see your problem.

Edit #2: Ok, take my original advice with the header, but use a define() rather than a variable. Those work inside functions after being included.

Upvotes: 7

cringe
cringe

Reputation: 13950

IIRC a common solution is a plain file that contains your declarations, that you include in every source file, something like 'constants.inc.php'. There you can define a bunch of application-wide variables that are then imported in every file.

Still, you have to provide the include directive in every single source file you use. I even saw some projects using this technique to provide localizations for several languages. I'd prefer the gettext way, but maybe this variant is easier to work with for the average user.

edit For your problem I recomment the use of $GLOBALS[], see Example #2 for details.

If that's still not applicable, I'd try to digg down PHP5 objects and create a static Singleton that provides needed static constants (http://www.developer.com/lang/php/article.php/3345121)

Upvotes: -1

Related Questions