PHP extract()Functions

PHP Array ReferenceComplete PHP Array Reference

Examples

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"; ?>

Run example »

Definition and Usage

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

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

This function returns the number of variables successfully set.


Syntax

extract(array,extract_rules,prefix)

Parameters Description
array Required. Specifies the array to use.
extract_rules Optional. The extract() function checks whether each key name is a valid variable name and also whether it conflicts with existing variable names in the symbol table. The handling of invalid and conflicting key names is determined by this parameter.

Possible values:

  • EXTR_OVERWRITE - Default. Overwrites existing variables if there is a conflict.
  • EXTR_SKIP - Does not overwrite existing variables if there is a conflict.
  • EXTR_PREFIX_SAME - If there is a conflict, prepend the prefix to the variable name.
  • EXTR_PREFIX_ALL - Prepends the prefix to all variable names.
  • EXTR_PREFIX_INVALID - Prepends the prefix only to invalid or numeric variable names.
  • EXTR_IF_EXISTS - Only overwrites the values of variables that already exist in the current symbol table. All others are ignored.
  • EXTR_PREFIX_IF_EXISTS - Creates prefixed variable names only when a variable with the same name already exists in the current symbol table. All others are ignored.
  • EXTR_REFS - Extracts variables as references. The imported variables still reference the values of the array parameter.
prefix Optional. If the value of the extract_rules parameter is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS, then prefix is required.

This parameter specifies the prefix. An underscore is automatically added between the prefix and the array key name.

Technical Details

Return value: Returns the number of variables successfully set.
PHP Version: 4+
Changelog: extract_rulesThe value EXTR_REFS was added in PHP 4.3.

extract_rulesThe values EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS were added in PHP 4.2.

Since PHP 4.0.5, this function returns the number of variables successfully set.

extract_rulesThe value EXTR_PREFIX_INVALID was added in PHP 4.0.5.

Since PHP 4.0.5,extract_rulesThe value EXTR_PREFIX_ALL also includes numeric variables.


More Examples

Example 1

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";
?>

Run example »


PHP Array ReferenceComplete PHP Array Reference Other Extensions