Some configurations can be expressed as arrays ('configuration parameter '=> 'configuration value',), for example:
Array
(
'Var1' => 'val1 ',
'Var2' => 'val2 ',
'Var3' => 'val3 ',
);
How to Use the configurations represented by these arrays?
First, add return to the front of the array and save it as a file separately:
Return Array
(
'Var1' => 'val1 ',
'Var2' => 'val2 ',
'Var3' => 'val3 ',
);
Then, in another file, require it will return the array, and then use the extract function to convert the array to a variable.
Example:
<? PHP
$ Catid = 1;
// Returns the Array
$ Var_array = require ('category _ '. $ catid.'. php ');
// Print_r ($ var_array );
// Convert the array into a variable
Extract ($ var_array, extr_prefix_same, "new ");
// Real variable value
Echo $ catid;
Echo $ new_catid;
Echo $ module;
Echo $ catname;
?>
Category_1.php file:
<? PHP
Return Array
(
'Catid' => '10 ',
'Module' => 'lightphp ',
'Type' => '1 ',
'Modelid' => '0 ',
'Catname' => 'website introduction ',
'Description' => '',
);
?>
-------------------------------------------------
References:
PHP Extract
Definition and usage
The PHP extract () function imports variables from the array to the current symbol table.
For each element in the array, the key name is used for the variable name, and the key value is used for the variable value.
The second parameter type is used to specify how the extract () function treats such conflicts when a variable already exists and an element with the same name exists in the array.
This function returns the number of successfully set variables.
Syntax
Extract (array, extract_rules, prefix)
Example
<? PHP
$ A = 'original ';
$ My_array = array ("A" => "cat", "B" => "dog", "C" => "horse ");
Extract ($ my_array );
Echo "\ $ A = $ A; \ $ B = $ B; \ $ c = $ C ";
?>
Output:
$ A = cat;
$ B = dog;
$ C = horse
Example 2
Convert the array key name to a variable and Output
<? PHP
// Assume $ var_array is the array returned by 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 ";
?>
The above example will output:
Blue, large, sphere, medium
$ Size is not overwritten. Because extr_prefix_same is specified, $ wddx_size is created.
If extr_skip is specified, $ wddx_size is not created.
Extr_overwrite will set the value of $ size to "medium ",
Extr_prefix_all creates a new variable $ wddx_color, $ wddx_size, and $ wddx_shape.
Zhang Qing (mesh), Weibo: http://t.qq.com/zhangking
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com
2013-4-2