mattyvx Posted January 2, 2011 Share Posted January 2, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/ Share on other sites More sharing options...
JD* Posted January 2, 2011 Share Posted January 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153727 Share on other sites More sharing options...
trq Posted January 2, 2011 Share Posted January 2, 2011 Don't use regular variables for this either. Use constants (see define). Variables are too easily overwritten and shouldn't be trusted. Globals in general should be avoided as much as possible. Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153733 Share on other sites More sharing options...
revraz Posted January 2, 2011 Share Posted January 2, 2011 Could also store this in a database and read it from there if you already have one setup. Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153743 Share on other sites More sharing options...
Anti-Moronic Posted January 2, 2011 Share Posted January 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153751 Share on other sites More sharing options...
mattyvx Posted January 2, 2011 Author Share Posted January 2, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153759 Share on other sites More sharing options...
revraz Posted January 2, 2011 Share Posted January 2, 2011 Use a text editor and delete it. How are you uploading your files? Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153765 Share on other sites More sharing options...
mattyvx Posted January 2, 2011 Author Share Posted January 2, 2011 sorted needed to change the encoding the ANSI! Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153771 Share on other sites More sharing options...
PFMaBiSmAd Posted January 2, 2011 Share Posted January 2, 2011 You need to use a good programming editor that allows you to save the file as a UTF-8 encoded WITHOUT the BOM or just save the file as an ANSI encoded file. Quote Link to comment https://forums.phpfreaks.com/topic/223166-setting-global-variable/#findComment-1153775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.