Open In App

PHP extract() Function

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.
Syntax

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 according 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.
Examples: 

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:
Example-1

PHP




<?php
      
    // input array
    $state = array("AS"=>"ASSAM", "OR"=>"ORISSA", "KR"=>"KERALA");
      
    extract($state);
      
    // after using extract() function
    echo"\$AS is $AS\n\$KR is $KR\n\$OR is $OR";
      
?>


Output:  

$AS is ASSAM
$KR is KERALA
$OR is ORISSA

Example-2

PHP




<?php
      
    $AS="Original";
      
    $state = array("AS"=>"ASSAM", "OR"=>"ORISSA", "KR"=>"KERALA");
      
    // 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";
      
?>


Output:  

$AS is Original
$KR is KERALA
$OR is ORISSA 
$dup_AS = ASSAM

Reference
http://php.net/manual/en/function.extract.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads