Skip to content
Related Articles

Related Articles

Improve Article
PHP | extract() Function
  • Last Updated : 08 Mar, 2018

The extract() Function is an inbuilt function in PHP. The extract() function does array to variable conversion. That is it converts array keys into variable names and array values into variable value. In other words, we can say that the extract() function imports variables from an array to the symbol table.


int extract($input_array, $extract_rule, $prefix)

Parameters: The extract() function accepts three parameters, out of which one is compulsory and other two are optional. All three parameters are described below:

  1. $input_array: This parameter is required. This specifies the array to use.
  2. $extract_rule: This parameter is optional. The extract() function checks for invalid variable names and collisions with existing variable names. This parameter specifies how invalid and colliding names will be treated. This parameter can take the following values:
    • EXTR_OVERWRITE: This rule tells that if there is a collision, overwrite the existing variable.
    • EXTR_SKIP: This rule tells that if there is a collision, don’t overwrite the existing variable.
    • EXTR_PREFIX_SAME: This rule tells that if there is a collision then prefix the variable name according to $prefix parameter.
    • EXTR_PREFIX_ALL: This rule tells that prefix all variable names accoring to $prefix parameter.
    • EXTR_PREFIX_INVALID: This rule tells that only prefix invalid/numeric variable names according to parameter $prefix.
    • EXTR_IF_EXISTS: This rule tells that to overwrite the variable only if it already exists in the current symbol table, otherwise do nothing.
    • EXTR_PREFIX_IF_EXISTS: This rule tells to create prefixed variable names only if the non-prefixed version of the same variable exists in the current symbol table.
  3. $prefix: This parameter is optional. This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character. Also this parameter is required only when the parameter $extract_rule is set to EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS.

Return Value: The return value of extract() function is an integer and it represents the number of variables successfully extracted or imported from the array.


Input : array("a" => "one", "b" => "two", "c" => "three")
Output :$a = "one" , $b = "two" , $c = "three"
Explanation: The keys in the input array will become the 
variable names and their values will be assigned to these
new variables.

Below programs illustrates working of extract() in PHP:


    // input array
    $state = array("AS"=>"ASSAM", "OR"=>"ORRISA", "KR"=>"KERELA");
    // after using extract() function
    echo"\$AS is $AS\n\$KR is $KR\n\$OR is $OR";




    $state = array("AS"=>"ASSAM", "OR"=>"ORRISA", "KR"=>"KERELA");
    // handling collisions with extract() function
    extract($state, EXTR_PREFIX_SAME, "dup");
    echo"\$AS is $AS\n\$KR is $KR\n\$OR if $OR \n\$dup_AS = $dup_AS";


$AS is Original
$dup_AS = ASSAM


My Personal Notes arrow_drop_up
Recommended Articles
Page :