How to use the PHP array as a configuration file

Source: Internet
Author: User
Tags php programming
This article introduces, the PHP array as a configuration file usage, a few small examples for everyone to learn the reference.

In PHP programming, it is often used in the form of arrays (' configuration parameters ' = = ' config value ') as a configuration file, for example:

Array (' var1 ' = ' val1 ', ' var2 ' = ' val2 ', ' var3 ' = ' val3 ',);

This article introduces the specific use of the array configuration file, for your reference. First, add a return to this array and save it as a single file:

Return Array (' var1 ' = ' val1 ', ' var2 ' = ' val2 ', ' var3 ' = ' val3 ',);

Then, require it in another file, it returns the array, and then converts the array to a variable using the Extract function. Example:

 
      

category_1.php file:

 
      ' Ten ',  ' module ' = ' lightphp ',  ' type ' = ' 1 ',  ' modelid ' = ' 0 ', ' catname ' and ' '  website introduction ',  ' Description ' = + ',);? >

The above code, using the Extract function, the following describes its usage. PHP extract defines and uses the PHP extract () function to import variables from an array into 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 conflicts when a variable already exists and the element with the same name in the array. This function returns the number of variables that were set successfully. Syntax Extract (Array,extract_rules,prefix) Example:

 
      "Cat", "b" = "Dog", "C" and "Horse"); Extract ($my _array); echo "\ $a = $a; \ $b = $b; \ $c = $c ";? >

Output: $a = Cat; $b = Dog; $c = Horse Example 2 The array key name is converted into a variable and output:

 
      "Blue", "size" = "medium", "shape" = "sphere"), Extract ($var _array, Extr_prefix_same, "WDDX"), echo "$color, $size , $shape, $wddx _sizen ";? >

Output: Blue, large, sphere, medium$size are not overwritten because the extr_prefix_same is specified, which makes $WDDX _size be built. If Extr_skip is specified, the $WDDX _size will not be established. Extr_overwrite will make the value of the $size "medium", Extr_prefix_all will create a new variable $wddx _color, $wddx _size and $wddx _shape.

  • Related Article

    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.