Easy Tutorial
❮ Func String Htmlspecialchars Decode Func Date Date Add ❯

PHP extract() Function

Complete PHP Array Reference Manual

Example

Assign the values "Cat", "Dog", and "Horse" to the variables $a, $b, and $c:

<?php
$a = "Original";
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>

Definition and Usage

The extract() function imports variables from an array into the current symbol table.

This function uses array keys as variable names and array values as variable values. For each element in the array, a corresponding variable is created in the current symbol table.

The function returns the number of variables successfully set.


Syntax

Parameter Description
array Required. Specifies the array to use.
extract_rules Optional. The extract() function will check if each key is a valid variable name and also check for conflicts with existing variables in the symbol table. The handling of invalid and conflicting key names will be determined by this parameter. Possible values: EXTR_OVERWRITE - Default. If there is a conflict, overwrite the existing variable. <br> EXTR_SKIP - If there is a conflict, do not overwrite the existing variable. <br> EXTR_PREFIX_SAME - If there is a conflict, prefix the variable name with prefix. <br> EXTR_PREFIX_ALL - Prefix all variable names with prefix. <br> EXTR_PREFIX_INVALID - Only prefix invalid or numeric variable names with prefix. <br> EXTR_IF_EXISTS - Only overwrite the values of existing variables in the current symbol table, otherwise do nothing. <br> EXTR_PREFIX_IF_EXISTS - Only create prefixed variable names if the non-prefixed version already exists in the current symbol table, otherwise do nothing. <br> EXTR_REFS - Extract variables as references. The imported variables will still reference the values of the array parameters.
prefix Optional. If the extract_rules parameter is set to EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS, then prefix is required. <br> <br> This parameter specifies the prefix. A underscore will be automatically added between the prefix and the array key.

Technical Details

Return Value: Returns the number of variables successfully set.
PHP Version: 4+
--- ---
Changelog: The extract_rules value EXTR_REFS was added in PHP 4.3. <br> <br> The extract_rules values EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS were added in PHP 4.2. <br> <br> Since PHP 4.0.5, this function returns the number of variables successfully set. <br> <br> The extract_rules value EXTR_PREFIX_INVALID was added in PHP 4.0.5. <br> <br> Since PHP 4.0.5, the extract_rules value EXTR_PREFIX_ALL also includes numeric variables.
--- ---

More Examples

Example 1

Using all parameters:

❮ Func String Htmlspecialchars Decode Func Date Date Add ❯