Search code examples
phpflat-file

PHP: performance oriented way of storing variables in a flat file?


I'm looking to store some basic information (just simple variables, not a database) which doesn't require any security (simple strings stored for case sensitivity, lets me skip a MySQL query to improve performance). I'm looking for quickest way to read the contents and create a few variables from those contents. I'm comfortable with how to interact with flat files for the most part, what I want to do is determine the most efficient way of storing a few variables and interpreting back in to PHP please.

PHP I'll store in the flatfile...

$case = 'My Site Name CaSiNG';
$string2 = 'some text';

Solution

  • This is fairly fast save for write blocking. I supposed you could also write your variables as strait php which should also be fast

    $variables = array();
    $variables['case'] = 'My site name';
    $variables['string2'] = 'some text';
    
    $write_to_file = serialize($variables);
    
    // todo: save $write_to_file
    // todo: open saved file and read contents to $write_to_file
    
    $vars = unserialize($write_to_file);
    print_r($vars);
    

    see Convert var_dump of array back to array variable to use var_dump to export all your variables and import them