PHP extract () function converts variables in data to PHP variables _php tutorial

Source: Internet
Author: User
The PHP extract () function imports variables from the 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.

Register the variable from the PHP array as a global variable, implement the key name as the variable name, and the value as the value of the variable, as follows

The code is as follows Copy Code
$vars = Array (' var1 ' = ' 1 ', ' var2 ' = ' 2 ', ' var3 ' = ' 3 ', ' var4 ' = ' 4 ', ' var5 ' = ' 5 ');

The implementation is accessed by means of a key name as a variable name, such as: $var 1, $var 2

The first scenario: Use the PHP built-in extract () function, as follows

The code is as follows Copy Code
Extract ($vars);

Second scenario: Register as a global variable with a foreach loop array, as follows

The code is as follows Copy Code
foreach ($vars as $k = = $v) {
$GLOBALS [$k] = $v;
}

The second scenario is recommended because of the performance and security issues with the extract () function.

Extract () function description

(PHP 3 >= 3.0.7, PHP 4, PHP 5)
Extract--Import variables from an array into the current symbol table

The code is as follows Copy Code

int extract (array var_array [, int extract_type [, string prefix]])

This function is used to import variables from the array into the current symbol table. Accept the associative array var_array as the argument and use the key name as the variable name, and the value as the value of the variable. For each key/value pair, the variables are created in the current symbol table and are affected by the Extract_type and prefix parameters.

Reference table

Parameters Description
Array Necessary. Specifies the input to use.
extract_rules

Optional. The extract () function checks to see if each key name is a valid variable name, and also checks whether the variable names in the symbol table conflict.

The handling of illegal, numeric, and conflicting key names is determined by this parameter. Can be one of the following values:

Possible values:

  • extr_overwrite-default. If there is a conflict, overwrite the existing variable.
  • Extr_skip-If there is a conflict, do not overwrite the existing variable. (ignores elements with the same name in the array)
  • extr_prefix_same-If there is a conflict, precede the variable name with the prefix PREFIX. Since PHP 4.0.5, this also includes the processing of the digital index.
  • extr_prefix_all-Prefix all variable names with PREFIX (the third argument).
  • extr_prefix_invalid-prefix PREFIX is only preceded by an illegal or numeric variable name. This tag is a new addition to PHP 4.0.5.
  • extr_if_exists-Overrides the value of a variable with the same name in the current symbol table only if it already exists. None of the others will deal with it. It can be used where a set of valid variables has been defined and then extracted from an array such as $_request to override those variables. This tag is a new addition to PHP 4.2.0.
  • extr_prefix_if_exists-A variable name with the prefix appended to it is created only if a variable with the same name already exists in the current symbol table, and none of the others are processed. This tag is a new addition to PHP 4.2.0.
  • extr_refs-Extracts the variable as a reference. This strongly demonstrates that the imported variable still references the value of the Var_array parameter. This flag can be used alone or in conjunction with any other flag in Extract_type. This tag is a new addition to PHP 4.3.0.
Prefix

Optional. Note that prefix is required only if the value of Extract_type is Extr_prefix_same,extr_prefix_all,extr_prefix_invalid or extr_prefix_if_exists. If the result appended with the prefix is not a valid variable name, it will not be imported into the symbol table.

An underscore is automatically added between the prefix and the array key name.

Use all parameters:

The code is as follows Copy Code

!--? php
$a = & #39; original& #39;
$my _array = Array ("A" => "Cat", "B" => "Dog", "C" => "Horse");

Extract ($my _array, Extr_prefix_same, & #39;d up& #39;);

Echo $a = $a; $b = $b; $c = $c; $dup _a = $dup _a; ";
?>

Output:

$a = Original; $b = Dog; $c = Horse; $dup _a = Cat;


http://www.bkjia.com/phpjc/445314.html Span id= "Indexurl" itemprop= "Indexurl" >www.bkjia.com true http://www.bkjia.com/phpjc/445314.html techarticle php Extract () The function imports variables from the 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 refer to ...

  • & #xe63a;-->

  • 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.