Definition and usage of the extract of function in PHP

  • 2020-05-19 04:28:18
  • OfStack

Definition and usage

The PHP extract() function imports variables from an array into 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, specifies how the extract() function handles such conflicts when a variable already exists and an array has elements of the same name.

This function returns the number of variables successfully set.

grammar
extract(array,extract_rules,prefix)

parameter describe array A necessity. Specify the input to use. extract_rules

Optional. The extract() function checks that each key name is a valid variable name, as well as whether it conflicts with the variable name in the symbol table.

The handling of illegal, numeric, and conflicting key names is determined based on this parameter. It can be 1 of the following values:

Possible values:

EXTR_OVERWRITE - default. If there is a conflict, override the existing variables. EXTR_SKIP - do not override existing variables if there is a conflict. (ignores elements with the same name in the array) EXTR_PREFIX_SAME - if there is a conflict, prefix the variable name with prefix. As of PHP 4.0.5, this also includes the processing of numeric indexes. EXTR_PREFIX_ALL - prefixes all variable names with prefix (third argument). EXTR_PREFIX_INVALID - only prefix prefix before illegal or numeric variable names. This mark is a new addition to PHP 4.0.5. EXTR_IF_EXISTS - overrides the value of variables with the same name only if they already exist in the current symbol table. Nothing else. This can be used when you have defined a valid set of variables, and you want to override them by extracting values from an array such as $_REQUEST. This tag is a new addition to PHP 4.2.0. EXTR_PREFIX_IF_EXISTS - only if a variable with the same name already exists in the current symbol table, create the variable name with the prefix attached, and do nothing else. This tag is a new addition to PHP 4.2.0. EXTR_REFS - extract the variable as a reference. This strongly indicates that the imported variable still references the value of the var_array parameter. You can use this logo alone or use OR in combination with any other logo in extract_type. This tag is a new addition to PHP 4.3.0. prefix

Optional. Please note that prefix is only required if the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the prefixed result 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.

Example 1

 
<?php 
$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:

 
<?php 
$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 looking at the code of 1, see a very useful function: extract (), it is the main function of the array, key name as a variable name, element value for a variable's value, can provide for the operation of the array and a convenient tool, for example, can easily extract $_POST or $_GET elements, to form the content of the submitted cannot without 11 assignment, directly using the following code:

form.html
 
<form action="action.php" method="post"> 
<input type="text" name="username"> 
<input type="password" name="password"> 
<input type="submit"> 

In action.php, just use the extract() function to unlock the $_POST global data:
action.php

 
<?php 
extract($_POST); 
// The equivalent of $username = $_POST['username']; 
//$password = $_POST['password']; 
?> 


Is it convenient? Well, here's the explanation in the PHP manual:

extract
(PHP 4, PHP 5)

extract - imports variables from an array into the current symbol table

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

This function is used to import variables from an array into the current symbol table. Take the combined array var_array as a parameter and treat the key name as the variable name and the value as the value of the variable. For each key/value pair, variables are created in the current symbol table and are affected by the extract_type and prefix parameters.


Note: this function returns the number of variables extracted since version 4.0.5.
Note: EXTR_IF_EXISTS and EXTR_IF_EXISTS were 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 existing variable names in the symbol table. The way illegal/numeric and conflicting key names are treated is determined by the extract_type parameter. It can be 1 of the following values:

EXTR_OVERWRITE
If there is a conflict, override the existing variables.
EXTR_SKIP
If there is a conflict, do not override the existing variables.
EXTR_PREFIX_SAME
If there is a conflict, prefix the variable name with prefix.
EXTR_PREFIX_ALL
Prefix all variable names with prefix. Since PHP 4.0.5 this also includes the processing of numeric indexes.
EXTR_PREFIX_INVALID
Only prefix the illegal/numeric variable name with prefix. This mark is a new addition to PHP 4.0.5.
EXTR_IF_EXISTS
Override their values only if variables of the same name already exist in the current symbol table. Nothing else. This can be used when you have defined a valid set of variables, and you want to override them by extracting values from an array such as $_REQUEST. This tag is a new addition to PHP 4.2.0.
EXTR_PREFIX_IF_EXISTS
Only if a variable of the same name already exists in the current symbol table, the variable name with the prefix is created, and nothing else is handled. This tag is a new addition to PHP 4.2.0.
EXTR_REFS
Extract variables as references. This strongly indicates that the imported variable still references the value of the var_array parameter. You can use this logo alone or use OR in combination with any other logo in extract_type. This mark 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 only required if the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the prefixed result 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 untrusted data, such as user 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 are not overridden, such as EXTR_SKIP, and be careful to extract them in the order defined by variables_order in php.ini.

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

extract Example # 1 () example
 
<?php 
/*  Assume that  $var_array  is  wddx_deserialize  Returned array */ 
$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, which causes $wddx_size to be established. If EXTR_SKIP is specified, $wddx_size will not be established. EXTR_OVERWRITE will cause the value of $size to be "medium", EXTR_PREFIX_ALL will create new variables $wddx_color, $wddx_size and $wddx_shape.

Associative arrays must be used, and numeric indexed arrays will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID are used.

Related articles: