PHP extract() Function

The extract() function is an inbuilt function in PHP that imports variables into the current symbol table from an array. The keys of the array become variable names, and the values become variable values. This function was introduced in PHP 4.0.

Syntax


int extract ( array $array [, int $flags = EXTR_OVERWRITE [, string $prefix = "" ]] );

                    

Parameters

Parameter Description Is Compulsory
array The array to extract variables from. Compulsory
flags Specifies how invalid/numeric keys and collisions are handled. Default is EXTR_OVERWRITE. Optional
prefix Used only if EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, or EXTR_PREFIX_INVALID is specified. Optional

Return Values

The extract() function returns the number of variables successfully imported into the symbol table.

PHP Example


<?php
$data = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");
extract($data);
echo "a: $a, b: $b, c: $c";
?>
                    

Output

a: Apple, b: Banana, c: Cherry

PHP Example


<?php
$data = array("a" => "Apple", "b" => "Banana", "c" => "Cherry", "a" => "Avocado");
extract($data, EXTR_PREFIX_SAME, "prefix");
echo "a: $a, prefix_a: $prefix_a, b: $b, c: $c";
?>
                    

Output

a: Avocado, prefix_a: Apple, b: Banana, c: Cherry