Dear folk ,

consider I define a constant value

<?php
  /*--------values.inc.php ------------*/
  define("Data","MyValue")
?>

so right now I want to change the Value of Data from another page and I wold like that to be saved on Values.inc.php file , you know I'm thinking some kind of data Store ,instead of using data bases.I have seen smarty template ENgine has done this before , could some on help me !!
let me clear , next time when I open the values.. file I would like to see

<?php
  /*--------values.inc.php ------------*/
  define("Data","AnotherValue")
?>

THanks

You can change the file itself, if you have write permission on it. Open the file and use a regex to replace the old value with the new.

could you tell me how this code look like !!
I'm not familiar with Regular Expression
thanks

you could get the file contents and then use str_replace.

$file = 'values.inc.php';
$fh = fopen( $file,'r' );
$data = fread( $file,filesize( $file ) );
fclose( $fh );
//or you can use file_get_contents()

$str = 'define("data","myValue");';
$with = 'define("data","newValue");';

$data = str_replace( $str,$with,$data );

$fh = fopen( $file,'w' );
fwrite( $fh,$data );
fclose( $fh );

Dear folk ,

consider I define a constant value

<?php
  /*--------values.inc.php ------------*/
  define("Data","MyValue")
?>

so right now I want to change the Value of Data from another page and I wold like that to be saved on Values.inc.php file , you know I'm thinking some kind of data Store ,instead of using data bases.I have seen smarty template ENgine has done this before , could some on help me !!
let me clear , next time when I open the values.. file I would like to see

<?php
  /*--------values.inc.php ------------*/
  define("Data","AnotherValue")
?>

THanks

I thought I'd add:

What you mention is common in PHP configuration files, or language files.

Usually, you know the names of the constants that you have saved in the file in advance. But if you don't, you could use an array, as it allows you to discover all the variables saved in it.

eg: file config.php

<?php

$config = array();

$config['name1'] = 'value1';
$config['name2'] = 'value2';

?>

Then when you want to add or remove something from your storage file, you can just include() the file, and get the array, modify it and write it back to file.

<?php

require_once('config.php');

$config['name1'] = 'newvalue1';

$contents = "

\$config = array();
\$config[name1] = '{$config[name1]}';
\$config[name2] = '{$config[name2]}';

";

file_put_contents('config.php', $contents);

?>

Note the forward slash before the $ signs lets PHP know that the $ sign is a literal instead of being the start of a variable.

It you don't know the names of the indexes in the array, then you can iterate through the array in a loop.
With constants, its harder to do this.

<?php

require_once('config.php');

$config['name1'] = 'newvalue1';

$contents = "\$config = array();";

foreach($config as $name=>$value) {
  $contents .= "\$config[$name] = '$value';";
}

file_put_contents('config.php', $contents);

?>

Another way of doing it is to create a Class, and define properties of that class. Example:

class Config {

  var $name1 = 'value1';
  var $name2 = 'value2';

}

Like an array, the properties of the class instance can be iterated over. Some consider this better because a class is automatically global while an array isn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.