Analysis of extract () functions in PHP-PHP Tutorial

Source: Internet
Author: User
The use of extract () functions in PHP. 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 the variable value. it can be said that a very useful function extract () is seen recently when you look at the code of a cool. its main function is to expand the array, the key name is used as the variable name, and the element value is the variable value. it can be said that it provides another convenient tool for array operations, for example, you can easily extract $ _ POST or $ _ GET elements. you cannot assign one-to-one 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.

Struct () is used to expand an array. The key name is used as the variable name and the element value is the variable value...

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.