tags:

views:

1009

answers:

10

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?

+5  A: 

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.

yjerem
A: 

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)

cringe
A: 

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.

Unkwntech
A: 

@Unkwntech:

As @Jeremy pointed out, that require solution won't work inside functions.

svec
A: 

Yes, because {$EL} is much less verbose than <br/>

Kevin
+2  A: 

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

TT
A: 

@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.
 }
Unkwntech
+1  A: 

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.

Brian Warshaw
A: 

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.

Darryl Hein
A: 

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.

Michał Rudnicki