PHP 数组配置文件如何用
Jun 13, 2016 am 11:19 AM
PHP 数组配置文件怎么用?
一些配置可以以数组的形式表示('配置参数' => '配置值',),例如:
array
(
'var1' => 'val1',
'var2' => 'val2',
'var3' => 'val3',
);
这些数组表示的配置怎么使用呢?
首先,把这个数组前面加上 return,单独保存为一个文件:
return array
(
'var1' => 'val1',
'var2' => 'val2',
'var3' => 'val3',
);
然后,在另一个文件里 require 它,就会返回该数组,然后再用 extract 函数把数组转化为变量。
例子:
$catid = 1;
//返回该数组
$var_array = require('category_' . $catid . '.php');
//print_r($var_array);
//把数组转化为变量
extract($var_array, EXTR_PREFIX_SAME, "new");
//现实变量的值
echo $catid;
echo $new_catid;
echo $module;
echo $catname;
?>
category_1.php文件:
return array
(
'catid' => '10',
'module' => 'lightPHP',
'type' => '1',
'modelid' => '0',
'catname' => '网站介绍',
'description' => '',
);
?>
-------------------------------------------------
参考资料:
PHP extract
定义和用法
PHP extract() 函数从数组中把变量导入到当前的符号表中。
对于数组中的每个元素,键名用于变量名,键值用于变量值。
第二个参数 type 用于指定当某个变量已经存在,而数组中又有同名元素时,extract() 函数如何对待这样的冲突。
本函数返回成功设置的变量数目。
语法
extract(array,extract_rules,prefix)
例子
$a = 'Original';
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
输出:
$a = Cat;
$b = Dog;
$c = Horse
例子2
数组键名转换成变量并输出
//假定 $var_array 是 wddx_deserialize 返回的数组
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_sizen";
?>
上例将输出:
blue, large, sphere, medium
$size 没有被覆盖,因为指定了 EXTR_PREFIX_SAME,这使得 $wddx_size 被建立。
如果指定了 EXTR_SKIP,则 $wddx_size 也不会被建立。
EXTR_OVERWRITE 将使 $size 的值为“medium”,
EXTR_PREFIX_ALL 将建立新变量 $wddx_color,$wddx_size 和 $wddx_shape。
张庆(网眼) ,微博:http://t.qq.com/zhangking
来自“网眼视界”:http://blog.why100000.com
“十万个为什么”电脑学习网:http://www.why100000.com
2013-4-2

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size

How to enable or disable taskbar thumbnail previews on Windows 11

How to change title bar color on Windows 11?

OOBELANGUAGE Error Problems in Windows 11/10 Repair

What are the differences between Huawei GT3 Pro and GT4?

10 Ways to Adjust Brightness on Windows 11
