2

I have some pages that share a common variable. At first, I just copied $max = 196; into each page to update it. This is obviously a bad way to do it as it's a lot more work and it's easy to make a mistake. I want to change it so I can change the value of this variable in a single place and everywhere use this value.

Right now, I put include 'rises.php'; on every page instead of the actual variable, and the contents of rises.php is <?php $max=196; ?>

I have also tried...

  • Saving 196 to a .txt file and each page opening it using `fopen().
  • Creating a small MySQL table which holds only this variable name and it's value.

So my question is...

What's the best way of managing a variable where multiple pages need to use it? In my case it's only 5-6 pages, but each page loads it a ton of times.

Cully
  • 484
  • 9
  • 19
  • See http://stackoverflow.com/q/2921469/1273830 what you need is probably a mutex lock. And, it's always a good idea to write reusable code, as did the answer on that question. – Prasanth Jun 18 '13 at 05:22
  • 2
    Having a configuration in a separate file is ok, sessions are completely irrelevant to the issue. So your current solution is acceptable and way better than sessions – zerkms Jun 18 '13 at 05:23
  • 1
    @zerkms is right - if it's just a value that you need to use in various files then chucking it in an include file is the way to go. Not sure why you feel the need to load it multiple times though - just include it once at the top of the page and then use it as needed. Unless you're modifying it each time you use it of course. – Mark Parnell Jun 18 '13 at 05:29
  • Will the value remain same throughout? Or do you need to update it in your pages and it should reflect in all pages? Is it a read only value? – Hanky Panky Jun 18 '13 at 05:44
  • No the value changes every few days. I want to change it in one place and all pages show the updated value. My current way does work, but I'm not sure if it's a good idea to have all those includes all over the place. – Cully Jun 18 '13 at 05:45
  • @MarkParnell Actually.. it loads a couple of times because it's in a loop, but I should really just pop it at the top of the page so it only loads once. So obvious, but I didn't even notice. Thanks. – Cully Jun 18 '13 at 05:47

3 Answers3

1

The best way is to create special Singleton class that has array (suggest $params) and then include this singleton and use params on every page. You must implement something like this http://code.google.com/p/data-registry/

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
  • This looks interesting. Is there any example code I could look at? The wiki on that link is blank so there's not much for me to go on. – Cully Jun 18 '13 at 05:41
  • 1
    In Download section you can find link http://data-registry.googlecode.com/files/Data_Registry.php – Michael Sivolobov Jun 18 '13 at 05:46
0

You can use session variable...

session_start();
$_SESSION['max'] = 96;
//to print your variable
echo $_SESSION['max'];
Neeraj Dangol
  • 223
  • 1
  • 12
  • Do I have to put `$_SESSION['max'] = 96;` on all of my pages? If so, then I'll just have to update it on all pages which makes this pointless. What am I missing here? – Cully Jun 18 '13 at 05:36
  • You don't have to. Just define $_SESSION['max'] in one page. Then, u can use this session variable in all pages. – Neeraj Dangol Jun 18 '13 at 10:08
0

PHP provides sessions which are a way to persist data between pages.

Here is how you use them:

  1. Add <?php session_start(); ?> at the very top of every page. It should be the absolute first line of your php file, even before any blank lines.

  2. In the page were you want to set a variable, do this $_SESSION['name'] = "value";

  3. In the page where you want to retrieve the variable, its the opposite: $foo = $_SESSION['name'];

PHP will take care of passing the value around. You don't have to include any special files or worry about databases.

Sessions are not permanent however. Its not a place to put settings and "constants". For that, your current solution is fine. For ephemeral data though, sessions are recommend.


"In the page were you want to set a variable"... That's the thing... any of these pages could be loaded first. The pages are all independant of one another. Where does it load the original variable?

Use auto_prepend_file to get around that problem.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Why not persist it in a file, like OP currently does? – zerkms Jun 18 '13 at 05:23
  • "You don't have to include any special files" --- but you do have to worry about having `session_start()` everywhere. So what's the difference then? – zerkms Jun 18 '13 at 05:24
  • "In the page were you want to set a variable"... That's the thing... any of these pages could be loaded first. The pages are all independant of one another. Where does it load the original variable? I don't see how this will help the problem I'm having. – Cully Jun 18 '13 at 05:38