可能大家也都见过很多开源的产品,大多它们的配置文件都存放在一个单独的文件中,而这个文件里只存放了一个数组,其实这里运用了一个PHP的小技巧,就是可以将文件包含进来,并且赋值给一个变量,这个变量就具有了整个配置的数组,我给大家举个例子:

config.php
--------------------------
<?php
return array(
'a' => 1,
'b' => 2,
'c' => 3,
);
?> getconfig.php
--------------------------
<?php
$config = require('config.php'); print_r($config);
?> 大家试一下,会发现$config变量里存储了config.php中的数组, 这就是运用了php可以把文件当作return函数的一种用法。
这种类似于ci的配置文件处理的方法

config.php
--------------------------
<?php

$config['ss'] = 100;
$config['sess'] = 200;
$config['sess_update'] = 300;

?>

getconfig.php
--------------------------
<?php
require('config.php'); print_r($config);
return array(
'master'=>array(
'master_host'=>'1',
'master_name'=>'1',
'master_password'=>'1',
'master_port'=>'1',
'slave1'=>array(
'slave_host'=>'2',
'slave_name'=>'2',
'slave_password'=>'2',
'slave_port'=>'2',
),
'slave2'=>array(
'slave_host'=>'3',
'slave_name'=>'3',
'slave_password'=>'3',
'slave_port'=>'3',
),
),
'master1'=>array(
'master_host'=>'5',
'master_name'=>'5',
'master_password'=>'5',
'master_port'=>'5',
'slave1'=>array(
'slave_host'=>'6',
'slave_name'=>'6',
'slave_password'=>'6',
'slave_port'=>'6',
),
'slave2'=>array(
'slave_host'=>'7',
'slave_name'=>'7',
'slave_password'=>'7',
'slave_port'=>'7',
),
), ); $config['db']['master']['host']= '';
$config['db']['master']['name']= '';
$config['db']['master']['password']= '';
$config['db']['master']['port']= '';
$config['db']['master']['slave1']['host']= '';
$config['db']['master']['slave1']['name']= '';
$config['db']['master']['slave1']['password']= '';
$config['db']['master']['slave1']['port']= '';
$config['db']['master']['slave2']['host']= '';
$config['db']['master']['slave2']['name']= '';
$config['db']['master']['slave2']['password']= '';
$config['db']['master']['slave2']['port']= ''; $config['db']['master1']['host']= '';
$config['db']['master1']['name']= '';
$config['db']['master1']['password']= '';
$config['db']['master1']['port']= '';
$config['db']['master1']['slave1']['host']= '';
$config['db']['master1']['slave1']['name']= '';
$config['db']['master1']['slave1']['password']= '';
$config['db']['master1']['slave1']['port']= '';
$config['db']['master1']['slave2']['host']= '';
$config['db']['master1']['slave2']['name']= '';
$config['db']['master1']['slave2']['password']= '';
$config['db']['master1']['slave2']['port']= ''; 多数据库主从配置为例

php读取文件里面的数组做为配置文件的更多相关文章

  1. java 读取文件的字节数组

    /*文件64位编码*/ public static void main(String[] args) {    byte[] fileByte = toByteArray(newFile);   St ...

  2. php以数组做为配置文件的读取和写入操作

    最近想用php开发个简单的文章管理系统,主要是做一批垃圾采集站,目前网上的cms都太多功能了,导致修改个模板要很多文件,花费很多功夫.开始用thinkphp框架做,感觉还是麻烦,后来改用ci,做好了后 ...

  3. bash内置命令mapfile:读取文件内容到数组

    bash提供了两个内置命令:readarray和mapfile,它们是同义词.它们的作用是从标准输入读取一行行的数据,然后每一行都赋值给一个数组的各元素.显然,在shell编程中更常用的是从文件.从管 ...

  4. C#读取文件为byte数组

    private byte[] FileContent(string fileName) { using (FileStream fs = new FileStream(fileName, FileMo ...

  5. c#逐行读取文件到数组

    /// <summary> /// 逐行读取文件到泛型数组 /// </summary> /// <param name="FilePath"> ...

  6. HTML5 文件域+FileReader 分段读取文件并上传到服务器(六)

    说明:使用Ajax方式上传,文件不能过大,最好小于三四百兆,因为过多的连续Ajax请求会使后台崩溃,获取InputStream中数据会为空,尤其在Google浏览器测试过程中. 1.简单分段读取文件为 ...

  7. HTML5 文件域+FileReader 分段读取文件(四)

    一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...

  8. C语言读取文件大量数据到数组

    针对.txt文档的大量有规律数据,譬如100行8列的数据将其读取到二维数组(矩阵)中,留作之后的数据处理. 改程序通过宏定义的方法来确定将要读取程序的行数和列数,将数据读取到二维数组data[100] ...

  9. PHP —— 读取文件到二维数组

    转自:PHP读取自定义ini文件到二维数组 读取文件,可以使用file_get_contents,file,parse_ini_file等,现在有一个需求,需要读取如下格式的文件: [food] ap ...

随机推荐

  1. Spring.Net Remoting 相关使用

    http://www.cnblogs.com/GoodHelper/archive/2009/11/19/SpringNet_Remoting.html  本博客系统转载  原文为 Spring.Se ...

  2. 转:Delphi 回调函数及例子

    http://anony3721.blog.163.com/blog/static/5119742010866050589/ { http://anony3721.blog.163.com/blog/ ...

  3. happypack 原理解析

    说起 happypack 可能很多同学还比较陌生,其实 happypack 是 webpack 的一个插件,目的是通过多进程模型,来加速代码构建,目前我们的线上服务器已经上线这个插件功能,并做了一定适 ...

  4. Codeforces Round #346 (Div. 2)

    前三题水 A #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int ...

  5. dl dt dd定义

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. sqlSQL2008如何创建定时作业

    SQL2008如何创建定时作业?此方法也适应于Sql Server2005数据库,有兴趣的可以来看下! 1.打开[SQL Server Management Studio],在[对象资源管理器]列表中 ...

  7. datagrid点击标题进行排序

    步骤: 1.页面上首先设置datagrid的AllowSorting="true",以及指定排序方法OnSortCommand="DataGrid1_SortComman ...

  8. HDU2191(多重背包)

    #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> ...

  9. 假装有题目 & Trie+贪心

    题意: 从N个数中选出两个使其异或值最大. SOL: 建立一个01字典树,然后对每一个数在树上贪心即可...Trie一个挺好的运用,复杂度O(n*n的位数) CODE: #include <cs ...

  10. 使用for( var each in record){} 去寻找object里面的内容;

    for(var each in object){ alert(each); }