| This article introduces how to use php arrays as a configuration file. There are several small examples for your reference.
In php programming, arrays ('configuration parameters' => 'configuration value') are often used as configuration files, for example: Array ('var1' => 'val1', 'var2' => 'val2', 'var3' => 'val3 ',);
This article describes how to use the array configuration file for your reference. 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:
Category_1.php file:
'10', 'module' => 'lightphp', 'type' => '1', 'modelid' => '0 ', 'catname' => 'website introduction', 'description' => '',);?>
The above code uses the extract function. The following describes its usage. PHP extract definition and usage 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:
"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 it:
"Blue", "size" => "medium", "shape" => "sphere"); extract ($ var_array, EXTR_PREFIX_SAME, "wddx"); echo "$ color, $ size, $ shape, $ wddx_sizen ";?>
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 sets the value of $ size to "medium", and EXTR_PREFIX_ALL creates a new variable $ wddx_color, $ wddx_size, and $ wddx_shape. |