Home > Article > Backend Development > Detailed explanation of the usage of extract based on PHP
extract
The function imports variables from an array into the current symbol table.
This function uses the array key name as the variable name and the array key value as the variable value. For each element in the array, a corresponding variable will be created in the current symbol table.
The second parameter type is used to specify how the extract() function treats such a conflict when a variable already exists and there is an element with the same name in the array.
This function returns the number of variables successfully imported into the symbol table.
Syntax
extract(array,extract_rules,prefix)
array
Required. Specifies the array to use.
extract_rules
Optional. The extract() function will check whether each key name is a legal variable name and also checks whether it conflicts with an existing variable name in the symbol table. The handling of illegal and conflicting key names will be determined based on this parameter.
Possible values:
EXTR_OVERWRITE - Default. If there is a conflict, existing variables are overwritten.
EXTR_SKIP - Do not overwrite existing variables if there is a conflict.
EXTR_PREFIX_SAME - If there is a conflict, prefix the variable name with prefix.
EXTR_PREFIX_ALL - Prefix all variable names with prefix.
EXTR_PREFIX_INVALID - Prefix only illegal or numeric variable names.
EXTR_IF_EXISTS - Overwrite the values of variables with the same name only if they already exist in the current symbol table. Others are not processed.
EXTR_PREFIX_IF_EXISTS - Only when a variable with the same name already exists in the current symbol table, a variable name with a prefix is created, and nothing else is processed.
EXTR_REFS - Extract variables as references. The imported variable still references the value of the array parameter.
prefix
Optional. 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 result after appending the prefix is not a legal variable name, it will not be imported into the symbol table.
An underscore will be automatically added between the prefix and the array key name.
Example 1, the value of the original array remains unchanged, and values are assigned to variables $a, $b, $c, $d, $e
$arr=array('a'=>1,'b'=>2,'c'=>3,'d'=>5,'e'=>6); extract($arr); print_r($arr); print_r($a); print_r($b); print_r($c); print_r($d); print_r($e);
Output
Array ( [a] => 1 [b] => 2 [c] => 3 [d] => 5 [e] => 6 ) 1 2 3 5 6
Recommended tutorial: "PHP Video Tutorial"
The above is the detailed content of Detailed explanation of the usage of extract based on PHP. For more information, please follow other related articles on the PHP Chinese website!