Extract () function of PHP in the magical analysis _php skills

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

Form.html
Copy Code code as follows:

<form action= "action.php" method= "POST" >
<input type= "text" name= "username" >
<input type= "password" name= "password" >
<input type= "Submit" >

Use the Extract () function in action.php to unlock the $_post global data:
action.php
Copy Code code as follows:

<?php
Extract ($_post);
Equivalent to $username = $_post[' username '];
$password = $_post[' password '];
?>

Is it convenient? Well, here are the detailed explanations in the PHP manual:

Extract
(PHP 4, PHP 5)

extract-import 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 a variable from an array into the current symbol table. Accepts the associative array Var_array as a parameter and takes the key name as the variable name, and the value as the value of the variable. For each key/value pair, variables are established in the current symbol table and are affected by Extract_type and prefix parameters.


Note: This function returns the number of variables fetched since version 4.0.5.

Note:extr_if_exists and extr_prefix_if_exists are introduced in the version 4.2.0.

Note:extr_refs is introduced in the 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 of treating illegal/numeric and conflicting key names will be 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, the existing variable is not overwritten.
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. This also includes processing of digital indexes since PHP 4.0.5.
Extr_prefix_invalid
Prefix prefix only on illegal/numeric variable names. This tag is a new addition to PHP 4.0.5.
Extr_if_exists
Overrides the value of a variable of the same name that already exists in the current symbol table. Nothing else is to be dealt with. You can use an instance where you have defined a set of valid variables and then extract the values from an array such as $_request to overwrite these variables. This tag is a new addition to PHP 4.2.0.
Extr_prefix_if_exists
When a variable with the same name already exists in the current symbol table, the name of the variable with the prefix appended is established and none of the others are processed. This tag is a new addition to PHP 4.2.0.
Extr_refs
Extracts a variable as a reference. This is a powerful indication 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 that prefix is required only if the Extract_type value is Extr_prefix_same,extr_prefix_all,extr_prefix_invalid or extr_prefix_if_exists. If the result appended with a 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 array key names.

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 an old code that relies on register_globals, make sure that you use extract_type values that are not overwritten, such as Extr_skip, and that you should be aware that you should follow php.ini by Variables_order The order in which the definitions are to be extracted.

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 Code code as follows:

<?php
/* Assume that the $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 covered because the extr_prefix_same is specified, which makes the $WDDX _size built. If Extr_skip is specified, the $WDDX _size will not be established. Extr_overwrite will make the value of $size "medium", Extr_prefix_all will create a new variable $wddx _color, $wddx _size and $wddx _shape.

An associative array must be used, and an array of numeric indices will not produce results unless extr_prefix_all or extr_prefix_invalid are used.

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.