How to use PHP arrays as configuration files

Source: Internet
Author: User
How to use PHP arrays as configuration files
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.

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.