The magical Analysis of extract () function in PHP _php tutorial

Source: Internet
Author: User
Recently, when looking at a cow's code, see a very useful function: Extract (), its main function is to expand the array, the key name as the variable name, the element value is the variable value, you can say that the operation of the array provides another convenient tool, for example, can be easily extracted $_post or $_ Get elements, the content submitted to the form can not be assigned one by one, directly using the following code:

Form.html
Copy CodeThe code is as follows:





In action.php, simply use the extract () function to unpack the $_post global data:
action.php
Copy CodeThe code is as follows:
Extract ($_post);
Equivalent to $username = $_post[' username '];
$password = $_post[' password ');
?>

Isn't it convenient? Oh, here is the PHP manual in the detailed explanation:

Extract
(PHP 4, PHP 5)

extract-importing variables from an array into 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 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.


Note: From version 4.0.5 This function returns the number of variables that were extracted.

Note:extr_if_exists and Extr_prefix_if_exists are introduced in version 4.2.0.

Note:extr_refs was introduced in version 4.3.0.

Extract () checks each key name to see if it can be used as a valid variable name and also checks for conflicts with variable names already in the symbol table. Methods that treat illegal/numeric and conflicting key names are determined according to the Extract_type parameter. Can be one of the following values:

Extr_overwrite
If there is a conflict, overwrite the existing variable.
Extr_skip
If there is a conflict, do not overwrite the existing variable.
Extr_prefix_same
If there is a conflict, precede the variable name with the prefix prefix.
Extr_prefix_all
Prefix all variable names with prefix. From PHP 4.0.5 This also includes the processing of the digital index.
Extr_prefix_invalid
Prefix prefix only with illegal/numeric variable names. This tag is a new addition to PHP 4.0.5.
Extr_if_exists
Overrides their values only if they already have a variable with the same name in the current symbol table. 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 a 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.
If Extract_type is not specified, it is assumed to be extr_overwrite.

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

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

Warning

Do not use Extract () for data that cannot be trusted, such as the user's input ($_get, ...). )。 If you do this, for example, to temporarily run old code that relies on register_globals, be sure to use extract_type values that will not be overwritten, such as Extr_skip, and be aware that you should follow the Variables_order in php.ini Define the order to extract.

One possible use of extract () is to import the contents of the associative array returned by Wddx_deserialize () into the symbol table variable.

Example#1 Extract () example
Copy CodeThe code is as follows:
/* Assume that $var _array is an 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 example above will output:

Blue, large, sphere, medium

The $size is not overwritten because 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.

You must use associative arrays, and arrays of numeric indexes will not produce results unless you use Extr_prefix_all or Extr_prefix_invalid.

http://www.bkjia.com/PHPjc/325668.html www.bkjia.com true http://www.bkjia.com/PHPjc/325668.html techarticle recently, when looking at a cow's code, see a very useful function: Extract (), its main function is to expand the array, the key name as the variable name, the element value is the variable value, you can say ...

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