views:

384

answers:

5

What's the easiest way of storing a single number on a server so that any script can access it, using PHP? Currently, I have a number stored in a file, but this seems somewhat inelegant.

+1  A: 

Your best bet would be to put it in a MySQL table for fetching later. That's probably the best way to store information in PHP.

Drahcir
Database over flat-file everyday.
Unkwntech
+4  A: 

Since your application probably already have some sort of include-file on the top "header.php" or simular, you could just create a constant/variable in that file, and all the files that include the header will also have access to the constant.

This may help you to define the constant: http://php.net/constant

Espo
+4  A: 

That depends on the characteristics of the number. Is it updated/modified often? Is it the only such number? If it isn't changed it's probably better to do as Espo suggests and store it as a php constant that can be included when necessary. If you have other such numbers you can put them all in that file. If they are updated often it's probably better to put it in the database.

But. If it's a single number, that is subject to change, and you don't forsee any need for storing other numbers, why not use a file? Just remember to use flock() when updating it to avoid concurrency issues.

Emil H
+5  A: 

There's no right answer here, but most modern PHP systems for building web applications have some kind of Configuration object. This is often implemented as a singleton

//brain dead config object
class NamespaceConfiguration {
 public static function getInstance() {
  if (!self::$instance instanceof self) { 
    self::$instance = new self;
  }
  return self::$instance;
 }

    public static function set($key,$value){
        //code to set $key/$value paid
    }

    public static function get($key){
       //code to get a $value based on a $key
    }
}

$config = NamespaceConfiguration::getInstance();
$config->set('myNumber',42);
....
function somewhereElse(){
    $config   = NamespaceConfiguration::getInstance();
    $myNumber = $config->set('myNumber');
}

This class is loaded on every page requiest. This gives every developer a standard API to call when they want to get or set single configuration values, and allows a single developer to control the where of storage and the how of retrieval (which may be a flat file, XML file, memory cache, MySQL database, XML file stored in a MySQL Database, XML File stored in a MySQL Database that contains a node which points to a file that contains the value, etc.)

The where and how of retrieval is going to depend on your application environment, although by using a configuration object you can create some efficiencies up front (storing already retrieved values in a property cache, pre-fetching certain values on instantiation, etc.)

Alan Storm
+1, that's what I would do in the case of a 'single number'
karim79
A: 

If it is a variable that is more of a environmental nature you could always use set those in Apache in your httpd.conf file you can set the following:

SetEnv myVar myValue

You can then use $_SERVER to fetch it

$_SERVER['myVar']

I usually set varibles on this if i am to setup same application on a few different virtual hosts but dont want them to have different parameters in the config file.

A config file will also meet your needs. There is some easy to use classes in Zend framework that can help out http://framework.zend.com/manual/en/zend.config.html more specific section 7.3 and 7.4 describes how you can write config parameters in a plain text file or in XML.

if you prefer plain old php you have the function parse_ini_file that lets you read in config parameters from a text file http://us.php.net/parse_ini_file

Iman