943,840 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1248
  • PHP RSS
Nov 19th, 2008
0

assing a value to DEfine

Expand Post »
Dear folk ,

consider I define a constant value


php Syntax (Toggle Plain Text)
  1. <?php
  2. /*--------values.inc.php ------------*/
  3. define("Data","MyValue")
  4. ?>

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)
  1. <?php
  2. /*--------values.inc.php ------------*/
  3. define("Data","AnotherValue")
  4. ?>


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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pedramphp is offline Offline
16 posts
since Sep 2007
Nov 20th, 2008
0

Re: assing a value to DEfine

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.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 729
Bite my shiny metal ass!
pritaeas is offline Offline
4,176 posts
since Jul 2006
Nov 20th, 2008
0

Re: assing a value to DEfine

could you tell me how this code look like !!
I'm not familiar with Regular Expression
thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pedramphp is offline Offline
16 posts
since Sep 2007
Nov 20th, 2008
0

Re: assing a value to DEfine

you could get the file contents and then use str_replace.

PHP Syntax (Toggle Plain Text)
  1. $file = 'values.inc.php';
  2. $fh = fopen( $file,'r' );
  3. $data = fread( $file,filesize( $file ) );
  4. fclose( $fh );
  5. //or you can use file_get_contents()
  6.  
  7. $str = 'define("data","myValue");';
  8. $with = 'define("data","newValue");';
  9.  
  10. $data = str_replace( $str,$with,$data );
  11.  
  12. $fh = fopen( $file,'w' );
  13. fwrite( $fh,$data );
  14. fclose( $fh );
Last edited by kkeith29; Nov 20th, 2008 at 6:05 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 21st, 2008
0

Re: assing a value to DEfine

Click to Expand / Collapse  Quote originally posted by pedramphp ...
Dear folk ,

consider I define a constant value


php Syntax (Toggle Plain Text)
  1. <?php
  2. /*--------values.inc.php ------------*/
  3. define("Data","MyValue")
  4. ?>

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)
  1. <?php
  2. /*--------values.inc.php ------------*/
  3. define("Data","AnotherValue")
  4. ?>


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 Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $config = array();
  4.  
  5. $config['name1'] = 'value1';
  6. $config['name2'] = 'value2';
  7.  
  8. ?>

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)
  1. <?php
  2.  
  3. require_once('config.php');
  4.  
  5. $config['name1'] = 'newvalue1';
  6.  
  7. $contents = "
  8.  
  9. \$config = array();
  10. \$config[name1] = '{$config[name1]}';
  11. \$config[name2] = '{$config[name2]}';
  12.  
  13. ";
  14.  
  15. file_put_contents('config.php', $contents);
  16.  
  17. ?>

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)
  1. <?php
  2.  
  3. require_once('config.php');
  4.  
  5. $config['name1'] = 'newvalue1';
  6.  
  7. $contents = "\$config = array();";
  8.  
  9. foreach($config as $name=>$value) {
  10. $contents .= "\$config[$name] = '$value';";
  11. }
  12.  
  13. file_put_contents('config.php', $contents);
  14.  
  15. ?>

Another way of doing it is to create a Class, and define properties of that class. Example:
PHP Syntax (Toggle Plain Text)
  1. class Config {
  2.  
  3. var $name1 = 'value1';
  4. var $name2 = 'value2';
  5.  
  6. }

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: fwrite not working with ob_start, Any ideas?
Next Thread in PHP Forum Timeline: php Speedy Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC