Jump to content

setting 'global' variable


mattyvx

Recommended Posts

Hello,

 

What I want to do is set some 'global' perameters for my site so I can standardise the code and make it easier to maintain.

 

For example;

 

Page1 has - "Advertise from £99 a month"

Page2 has - "for just £99 a month..."

 

I would like to have a page (say globals.php) which defines global perameters which I can then use anywhere on my site;

e.g.

 

$global-variable['ad_price'] = "99";

 

 

Then each page could read  - "Advertise from £  $global-variable['ad_price']; a month"

 

I know I can do this by including the "globals.php" file on every page but can I do it without using includes?

Link to comment
Share on other sites

The only other way I can think of would be to use sessions, where you would have code at the top of every page to initiate a session and check to see if the session is set for price ($_SESSION['ad_price']) and if not, query a database for the current value, then set the session. It's pretty much the same thing as doing an include.

 

Of course, you could do the same thing by reading a text file on your server instead, but at the end of the day, it's all the same; the value has to come from somewhere, and if you want it to be centrally located, you're going to have to tell all of your pages where to find that information

Link to comment
Share on other sites

Depending on how many variables, you might consider storing this data in the database. You can store LOTS of variables in a single column using JSON.

 

Again, depending on how many variables I would consider using constants as sloppy. You can see how some people manage this in the language files for popular php apps like this forum for example.

 

However, if you do require these variables in classes and such you have to globalize them which is bad practice. From what I can tell, you don't mean globalizing variables in that sense, you mean defining a ton of variables (or an array) in a file which you can include in the scripts which need the values. THAT'S ok.

 

Another idea is wrap the whole thing in a class, and have a kind of global array/variable manager.

 

Oh, and you can't use '-' in your variable names.

 

Using includes to do this is THE easiest, fastest way. Either that, or declare them in each script, or pull them from a database. A quick include can't be matched.

Link to comment
Share on other sites

you mean defining a ton of variables (or an array) in a file which you can include in the scripts which need the values. THAT'S ok.

 

Oh, and you can't use '-' in your variable names.

 

Using includes to do this is THE easiest, fastest way. Either that, or declare them in each script, or pull them from a database. A quick include can't be matched.

 

Yes, it's more of a continuity and timesaving feature, I run several websites and some of them share code modules. If i make an update to one I can't copy and paste the code over to the others because there are different variables i.e. domain name or advertising price. I figured if I had a global variable which was defined I'd be able to copy and paste the updated modules across and just change the "global.php" file without having to trawl through all the lines of code.

 

Right, I have create a globals.php file and included it on the required pages via;

 

<?php 
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();  
include_once $_SERVER['DOCUMENT_ROOT'] . '/scripts/globals.php';
?>

 

The code passes the variables fine HOWEVER, i'm getting a BOM on my pages now. ( and a linespace is displayed)....how do I get rid of this? Is it something to do with setting the encoding for the globals.php file?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.