<p>I want to define something like this in <em>php</em>:</p>

<pre><code>$EL = "\n&lt;br /&gt;\n";
</code></pre>

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

<pre><code>echo "Blah blah blah{$EL}";
</code></pre>

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

[{'answer_id': 13003, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 4, 'selected': True, 'text': '<p>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:</p>\n\n<pre><code> include \'header.php\';\n</code></pre>\n\n<p>you won\'t have to use the global keyword or anything, the second line of code you wrote should work.</p>\n\n<p>Edit: Oh sorry, that won\'t work inside functions... now I see your problem.</p>\n\n<p>Edit #2: Ok, take my original advice with the header, but use a <a href="http://php.net/define" rel="noreferrer">define()</a> rather than a variable. Those work inside functions after being included.</p>\n'}, {'answer_id': 13005, 'author': 'cringe', 'author_id': 834, 'author_profile': 'https://Stackoverflow.com/users/834', 'pm_score': -1, 'selected': False, 'text': '<p>IIRC a common solution is a plain file that contains your declarations, that you include in every source file, something like \'<em>constants.inc.php</em>\'. There you can define a bunch of application-wide variables that are then imported in every file.</p>\n\n<p>Still, you have to provide the include directive in <strong>every</strong> 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.</p>\n\n<p><strong>edit</strong> For your problem I recomment the use of <strong>$GLOBALS[]</strong>, see <a href="http://php.net/manual/en/language.variables.scope.php" rel="nofollow noreferrer">Example #2</a> for details.</p>\n\n<p>If that\'s still not applicable, I\'d try to digg down PHP5 objects and create a static Singleton that provides needed static constants (<a href="http://www.developer.com/lang/php/article.php/3345121" rel="nofollow noreferrer">http://www.developer.com/lang/php/article.php/3345121</a>)</p>\n'}, {'answer_id': 13008, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': -1, 'selected': False, 'text': '<p>Sessions are going to be your best bet, if the data is user specific, else just use a conifg file.\nconfig.php:</p>\n\n<pre><code>&lt;?php\n$EL = "\\n&lt;br /&gt;\\n";\n?&gt;\n</code></pre>\n\n<p>Then on each page add</p>\n\n<pre><code>require \'config.php\'\n</code></pre>\n\n<p>the you will be able to access $EL on that page.</p>\n'}, {'answer_id': 13058, 'author': 'TT.', 'author_id': 868, 'author_profile': 'https://Stackoverflow.com/users/868', 'pm_score': 2, 'selected': False, 'text': '<p>Sounds like the job of a constant. See the function <a href="http://us2.php.net/define" rel="nofollow noreferrer">define()</a>.</p>\n'}, {'answer_id': 13075, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 0, 'selected': False, 'text': "<p>@svec yes this will, you just have to include the file inside the function also.  This is how most of my software works.</p>\n\n<pre><code>function myFunc()\n {\nrequire 'config.php';\n//Variables from config are available now.\n }\n</code></pre>\n"}, {'answer_id': 13094, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 1, 'selected': False, 'text': '<p>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 </p>\n\n<pre><code>MyClass::MY_CONST\n</code></pre>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>If I may offer a suggestion, it would be avoiding this sort of echoing that requires echoing tags (like <code>&lt;br /&gt;</code>). 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</p>\n\n<pre><code>echo "Blah Blah Blah\\n&lt;br /&gt;\\n";\n</code></pre>\n\n<p>try:</p>\n\n<pre><code>&lt;?php\nif($condition) {\n?&gt;\n&lt;p&gt;Blah blah blah\n&lt;br /&gt;\n&lt;/p&gt;\n&lt;?php\n}\n?&gt;\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 53094, 'author': 'Darryl Hein', 'author_id': 5441, 'author_profile': 'https://Stackoverflow.com/users/5441', 'pm_score': 0, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>&lt;?php\n\nclass SITE {\n    public static $el;\n}\n\nSITE::$el = "\\n&lt;br /&gt;\\n";\n\nfunction Test() {\n    echo SITE::$el;\n}\n\nTest();\n\n?&gt;\n</code></pre>\n\n<p>This will output <code>&lt;br /&gt;</code></p>\n\n<p>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.</p>\n\n<p>This was suggested to my by a user on the <a href="http://forum.nusphere.com" rel="nofollow noreferrer">PhpEd forums</a>.</p>\n'}, {'answer_id': 53672, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 0, 'selected': False, 'text': '<p>svec, use a PHP framework. Just any - there\'s plenty of them out there.\nThis is the right way to do it. With framework you have single entry\npoint for your application, so defining site-wide variables is easy and\nnatural. Also you don\'t need to care about including header files nor\nchecking if user is logged in on every page - decent framework will do\nit for you.</p>\n\n<p>See:</p>\n\n<ul>\n<li><a href="http://framework.zend.com/" rel="nofollow noreferrer">Zend framework</a></li>\n<li><a href="http://cakephp.org/" rel="nofollow noreferrer">CakePHP</a></li>\n<li><a href="http://www.symfony-project.org/" rel="nofollow noreferrer">Symfony</a></li>\n<li><a href="http://kohanaphp.com/" rel="nofollow noreferrer">Kohana</a></li>\n</ul>\n\n<p>Invest some time in learning one of them and it will pay back very soon.</p>\n'}, {'answer_id': 4432971, 'author': 'dbwebtek', 'author_id': 538362, 'author_profile': 'https://Stackoverflow.com/users/538362', 'pm_score': 2, 'selected': False, 'text': "<p>Do this\n<code>\ndefine ('el','\\n\\&lt;\\br/>\\n');\n</code>\nsave it as el.php</p>\n\n<p>then you can include any files you want to use, i.e</p>\n\n<p>echo 'something'.el;  // note I just add el at end of line or in front</p>\n\n<p>Hope this help</p>\n\n<p>NOTE please remove the '\\' after &lt; br since I had to put it in or it wont show br tag on the answer...</p>\n"}, {'answer_id': 59923336, 'author': 'Progrock', 'author_id': 3392762, 'author_profile': 'https://Stackoverflow.com/users/3392762', 'pm_score': 0, 'selected': False, 'text': '<p>You can use the <a href="https://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file" rel="nofollow noreferrer">auto_prepend_file directive</a> 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.</p>\n\n<p>So if your prepend file contains:</p>\n\n<pre><code>&lt;?php\ndefine(\'FOO\', \'badger\');\n</code></pre>\n\n<p>In another Php file you could access the constant:</p>\n\n<pre><code>echo \'this is my \'. FOO;\n</code></pre>\n'}, {'answer_id': 63679111, 'author': 'Ezekiel Arin', 'author_id': 14135718, 'author_profile': 'https://Stackoverflow.com/users/14135718', 'pm_score': 0, 'selected': False, 'text': "<p>You might consider using a framework to achieve this. Better still you can use</p>\n<pre><code>Include 'functions.php';\nrequire('functions');\n</code></pre>\n<p>Doing OOP is another alternative</p>\n"}]
['https://Stackoverflow.com/questions/13000', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/103/']