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
- < FormAction = "action. php" method = "post">
- < InputType = "text" name = "username">
- < InputType = "password" name = "password">
- < InputType = "submit">
In action. php, you only need to use the PHP function extract () to unbind the $ _ POST global data:
Action. php
- < FormAction = "action. php" method = "post">
- < InputType = "text" name = "username">
- < InputType = "password" name = "password">
- < InputType = "submit">
Is it convenient? The following is a detailed explanation of the PHP function extract () 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])
The PHP function extract () 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.
The PHP function 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 the PHP function 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 PHP function extract () is to import the content returned by wddx_deserialize () in the combined array to the symbol table variable.
Example #1 extract () Example
- <? Php
- /* Assume that $ var_array is wddx _
Array returned by deserialize */
- $ Size = "large ";
- $ Var_array = array ("color" => "blue ",
- "Size" => "medium ",
- "Shape" => "sphere ");
- Extract ($ var_array, EXTR _
PREFIX_SAME, "wddx ");
- Echo "$ color, $ size, $ shape, $ wddx_sizen ";
- ?>
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 PHP function extract () must use an associated array. arrays of numeric indexes will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used.