Definitions and usage of extract () functions in PHP _ PHP Tutorial

Source: Internet
Author: User
The definition and usage of the extract () function in PHP. Define and use the PHPextract () function to import 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. Second parameter 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)

Parameters Description
Array Required. Specifies the input to be used.
Extract_rules

Optional. The extract () function checks whether each key name is a valid variable name and whether it is in conflict with the variable name in the symbol table.

The processing of illegal, numbers, and conflicting key names is determined by this parameter. It can be one of the following values:

Possible values:

  • EXTR_OVERWRITE-default. If a conflict exists, the existing variables are overwritten.
  • EXTR_SKIP-if there is a conflict, the existing variables are not overwritten. (Ignore elements with the same name in the array)
  • EXTR_PREFIX_SAME-if there is a conflict, add the prefix before the variable name. Since PHP 4.0.5, this also includes processing digital indexes.
  • EXTR_PREFIX_ALL-prefix all variable names (the third parameter ).
  • EXTR_PREFIX_INVALID-only prefix before invalid or numeric variable names. This mark is newly added to PHP 4.0.5.
  • EXTR_IF_EXISTS-only overwrite the values of variables with the same name in the current symbol table. None of them are processed. It can be used for variables that have defined a combination, and then extract values from an array such as $ _ REQUEST to overwrite these variables. This mark is newly added to PHP 4.2.0.
  • EXTR_PREFIX_IF_EXISTS. This mark is newly added to PHP 4.2.0.
  • EXTR_REFS-extract variables as references. This effectively demonstrates that the imported variable still references the value of the var_array parameter. This flag can be used independently OR in extract_type OR with any other flag. This mark is newly added to PHP 4.3.0.
Prefix

Optional. Note that prefix is only required when the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result with a prefix is not a valid variable name, it is not imported to the symbol table.

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

Example 1

The code is as follows:


$ 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

Use all parameters:

The code is as follows:


$ A = 'original ';
$ My_array = array ("a" => "Cat", "B" => "Dog", "c" => "Horse ");

Extract ($ my_array, EXTR_PREFIX_SAME, 'dup ');

Echo "\ $ a = $ a; \ $ B = $ B; \ $ c = $ c; \ $ dup_a = $ dup_a ;";
?>



Output:

$ A = Original;
$ B = Dog;
$ C = Horse;
$ Dup_a = Cat;

PHP extract () function

Recently, when reading a cool man's code, I saw a very useful function: extract (). its main function is to expand the array and use the key name as the variable name, the element value is a variable value, which provides another convenient tool for Array Operations. for example, you can easily extract $ _ POST or $ _ GET elements, you cannot assign values to the content submitted in the form. you can directly use the following code:

Form.html

The code is as follows:







In action. php, you only need to use the extract () function to unbind the $ _ POST global data:
Action. php

The code is as follows:


Extract ($ _ POST );
// Equivalent to $ username = $ _ POST ['username'];
// $ Password = $ _ POST ['password'];
?>



Is it convenient? The following is a detailed explanation in the PHP manual:

Extract
(PHP 4, PHP 5)

Extract-import the variable from the array to the current symbol table

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

This function is used to import variables from the array to the current symbol table. The array var_array is used as the parameter and the key name is used as the variable name, and the value is used as the variable value. Each key/value pair will create a variable in the current symbol table and be affected by the extract_type and prefix parameters.


Note: This function returns the number of extracted variables from version 4.0.5.
Note: EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS are introduced in version 4.2.0.
Note: EXTR_REFS is introduced in version 4.3.0.

Extract () checks each key name to see if it can be used as a valid variable name. it also checks for conflicts with existing variable names in the symbol table. The method for dealing with illegal/numbers and conflicting key names is determined by the extract_type parameter. It can be one of the following values:

EXTR_OVERWRITE
If there is a conflict, overwrite the existing variables.
EXTR_SKIP
If there is a conflict, the existing variables are not overwritten.
EXTR_PREFIX_SAME
If there is a conflict, add the prefix before the variable name.
EXTR_PREFIX_ALL
Prefix all variable names. Since PHP 4.0.5, this also includes processing digital indexes.
EXTR_PREFIX_INVALID
Prefix is added only before invalid/numeric variable names. This mark is newly added to PHP 4.0.5.
EXTR_IF_EXISTS
Override the values of variables with the same name only in the current symbol table. None of them are processed. It can be used for variables that have defined a combination, and then extract values from an array such as $ _ REQUEST to overwrite these variables. This mark is newly added to PHP 4.2.0.
EXTR_PREFIX_IF_EXISTS
Only when a variable with the same name already exists in the current symbol table, the variable name with the prefix is created. other variables are not processed. This mark is newly added to PHP 4.2.0.
EXTR_REFS
Extract variables as references. This effectively demonstrates that the imported variable still references the value of the var_array parameter. This flag can be used independently OR in extract_type OR with any other flag. This mark is newly added to PHP 4.3.0.
If extract_type is not specified, it is assumed to be EXTR_OVERWRITE.

Note that prefix is only required when the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result with a prefix is not a valid variable name, it is not imported to the symbol table. An underline is automatically added between the prefix and the array key name.

Extract () returns the number of variables successfully imported into the symbol table.

Warning

Do not use extract () for untrusted data, such as user input ($ _ GET ,...). If you do this, for example, to temporarily run the old code dependent on register_globals, make sure that the extract_type value that will not be overwritten is used, such as EXTR_SKIP, and pay attention to php. the order defined by variables_order in ini is extracted.

One possible use of extract () is to import the content returned by wddx_deserialize () in the Combined Array to the symbol table variable.

Example #1 extract () Example

The code is as follows:


/* Assume that $ 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_size \ n ";
?>


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 sets the value of $ size to "medium", and EXTR_PREFIX_ALL creates a new variable $ wddx_color, $ wddx_size, and $ wddx_shape.

The associated array must be used. the array of the numeric index will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used.

The export 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...

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.