| | |
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
| Thread Tools | Search this Thread |
apache api array autocomplete beginner binary body broken cakephp class cms code cron curl database dataentry date date/time duplicates dynamic ebooks email emptydisplayvalue error execute explodefunction file firstoptioninphpdroplist folder form forms function functions google href htaccess html image include ip javasciptvalidation javascript joomla keywords limit link list login matching mediawiki menu msqli_multi_query multiple mycodeisbad mysql navigation number oop parameter parsing paypal pdf php phpincludeissue query random recourse recursive regex script search seo server sessions shot source sp space speed sql static subdomain subscription system table tag tutorial tutorials update upload url validator variable vbulletin video web webdesign white xml youtube






