What is PHP extract() Function?
If you want to import values of an associative array to the current symbol table, use extract() function. From each array element, the function creates one variable in the symbol table. One array key becomes a variable name and its element becomes variable’s value.
Syntax:
extract(array, flag, prefix)
Parameters:
The Function has 1 required parameter and 2 optional parameters-
array (Required): It is the input array.
flag (Optional): It controls how the extract function handles invalid variable names and collisions with existing variables that are exist in the current symbol table. It has the following values-
- EXTR_OVERWRITE (Default) – It overwrites existing variable with the array’s value. Check example 2.
- EXTR_SKIP – It doesn’t overwrite existing variable with the array’s value. Check example 3.
- EXTR_PREFIX_SAME – It doesn’t overwrite the existing variable and it creates a new variable with appending the specified prefix. Check example 4.
- EXTR_PREFIX_ALL – It prefixes all the variables with prefix.
- EXTR_PREFIX_INVALID – It add prefix only invalid/numeric variables.
- EXTR_IF_EXISTS – It only overwrites a variable if it already exists.
- EXTR_PREFIX_IF_EXISTS – It only adds prefix if the same variable exists.
- EXTR_REFS – It extracts variables as references.
prefix (Optional): This parameter is required if these flags are used in the 2nd parameter- EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID and EXTR_PREFIX_IF_EXISTS.
Return Values:
The function returns the number of variables successfully extracted or imported from the array. Check example 5.
Examples:
Example 1:
<?php
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascripot", "Serverside" => "PHP");
extract($languages);
echo "Markup: $Markup, Styling: $Styling, Serverside: $Serverside";
?>
Output:
Markup: HTML, Styling: CSS, Serverside: PHP
Example 2:
<?php
$Serverside = "Python";
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascripot", "Serverside" => "PHP");
extract($languages, EXTR_OVERWRITE);
echo "Markup: $Markup, Styling: $Styling, Serverside: $Serverside";
?>
Output:
Markup: HTML, Styling: CSS, Serverside: PHP
Example 3:
<?php
$Serverside = "Python";
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascripot", "Serverside" => "PHP");
extract($languages, EXTR_SKIP); echo "<br>";
echo "Markup: $Markup, Styling: $Styling, Serverside: $Serverside";
?>
Output:
Markup: HTML, Styling: CSS, Serverside: Python
Example 4:
<?php
$Serverside = "Python";
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascripot", "Serverside" => "PHP");
extract($languages, EXTR_PREFIX_SAME, "new"); echo "<br>";
echo "Markup: $Markup, Styling: $Styling, Serverside: $Serverside, More Serverside: $new_Serverside" ;
?>
Output:
Markup: HTML, Styling: CSS, Serverside: Python, More Serverside: PHP
Example 5:
<?php
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascripot", "Serverside" => "PHP");
echo “Number of variables with imported is: ” . extract($languages);
?>
Output:
Number of variables with imported is: 4
Caution:
Try avoiding the extract() function when getting data from $_GET, $_POST, or $FILE because it may hamper data security from attackers. Though, you can use non-overwriting flags like EXTR_SKIP or EXTR_IF_ELSES.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP extract() Function
The extract() function is a built-in PHP function and part of the PHP’s built-in array functions.