How does php modify the configuration file?

Source: Internet
Author: User
How does php modify the configuration file Config. php code as follows: & nbsp; & lt ;? Phpreturn & nbsp; array (& nbsp; 'version' & nbsp ;=& gt; & nbsp; '100 php: how to modify the configuration file
The Config. php code is as follows:
 
 
return array(
        'version' => '20120823',
'secretKey' => '92fe5927095eaac53cd1aa3408da8135',
'areaname' => 'China',
);


Now I want to write a Common. php setConfig ($ fileName, $ value) method to modify the value of areaname. $ FileName is the name of the Config. php file, and $ value is the replaced value. I don't know how to write it ?? Php configuration file array
------ Solution --------------------
File_get_contents ()
File_put_contents ()
------ Solution --------------------
function setConfig($fileName, $value) {
  ob_start();
  $a = @include($fileName);
  ob_end_clean();
  if(! is_array($a)) trigger_error("Invalid data file", E_USER_ERROR);
  $a['areaname'] = $value;
  file_put_contents($fileName, ' }

setConfig('config.php', 'aaa');

However, due to the limitations of your solution, this function is not common.
For example, you cannot modify the secretKey.
It is recommended to rewrite
function setConfig($key, $value, $fileName='config.php') {
  ob_start();
  $a = @include($fileName);
  ob_end_clean();
  if(! is_array($a)) trigger_error("Invalid data file", E_USER_ERROR);
  $a[$key] = $value;
  file_put_contents($fileName, ' }

setConfig('areaname', 'bbb');

------ Solution --------------------
There is a positive solution upstairs. good developers must have an idea on universality.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.