| | |
assing a value to DEfine
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2007
Posts: 16
Reputation:
Solved Threads: 0
Dear folk ,
consider I define a constant value
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
THanks
consider I define a constant value
php Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?php /*--------values.inc.php ------------*/ define("Data","AnotherValue") ?>
THanks
Last edited by peter_budo; Nov 20th, 2008 at 1:10 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
you could get the file contents and then use str_replace.
PHP Syntax (Toggle Plain Text)
$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 );
Last edited by kkeith29; Nov 20th, 2008 at 6:05 pm.
•
•
•
•
Dear folk ,
consider I define a constant value
php Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?php /*--------values.inc.php ------------*/ define("Data","AnotherValue") ?>
THanks
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 Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?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:
PHP Syntax (Toggle Plain Text)
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: fwrite not working with ob_start, Any ideas?
- Next Thread: php Speedy Problem
Views: 724 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access address ajax apache api array autoincrement beginner binary broken cakephp checkbox class cms code cron curl database date dehasher directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu method methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure support! syntax system table tutorial update updates upload url validation validator variable video web xml youtube






