Why extract ()
Yes, we need readable code. Hire a resource who can read better.
Usually, in our code, when we use arrays as params in a function, using extract() function in php makes it very easy to parse each and every array value in a variable. It has both pros and cons which i have tried to cover in the blog. Give it a Try!
extract (PHP 4, PHP 5)
Import variables into the current symbol table from an array.
int extract ( array&$array
[, int$flags
= EXTR_OVERWRITE [, string$prefix
=NULL
]] )
Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table.
What is symbol Table
In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program’s source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location.
An example of symbol table has been given below:
Symbol name | Type | Scope |
---|---|---|
bar |
function, double | extern |
x |
double | function parameter |
foo |
function, double | global |
count |
int | function parameter |
sum |
double | block local |
i |
int | for-loop statement |
Parameters
array
-
An associative array. This function treats keys as variable names and values as variable values. For each key/value pair it will create a variable in the current symbol table, subject to
flags
andprefix
parameters.You must use an associative array; a numerically indexed array will not produce results unless you use
EXTR_PREFIX_ALL
orEXTR_PREFIX_INVALID
. flags
-
The way invalid/numeric keys and collisions are treated is determined by the extraction
flags
. It can be one of the following values:EXTR_OVERWRITE
- If there is a collision, overwrite the existing variable.
EXTR_SKIP
- If there is a collision, don’t overwrite the existing variable.
EXTR_PREFIX_SAME
- If there is a collision, prefix the variable name with
prefix
. EXTR_PREFIX_ALL
- Prefix all variable names with
prefix
. EXTR_PREFIX_INVALID
- Only prefix invalid/numeric variable names with
prefix
. EXTR_IF_EXISTS
- Only overwrite the variable if it already exists in the current symbol table, otherwise do nothing. This is useful for defining a list of valid variables and then extracting only those variables you have defined out of $_REQUEST, for example.
EXTR_PREFIX_IF_EXISTS
- Only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table.
EXTR_REFS
- Extracts variables as references. This effectively means that the values of the imported variables are still referencing the values of the
array
parameter. You can use this flag on its own or combine it with any other flag by OR’ing theflags
.
If
flags
is not specified, it is assumed to beEXTR_OVERWRITE
. prefix
-
Note that
prefix
is only required ifflags
isEXTR_PREFIX_SAME
,EXTR_PREFIX_ALL
,EXTR_PREFIX_INVALID
orEXTR_PREFIX_IF_EXISTS
. If the prefixed result is not a valid variable name, it is not imported into the symbol table. Prefixes are automatically separated from the array key by an underscore character.
Return Values
Returns the number of variables successfully imported into the symbol table.
Example 1
<?php
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "prefix");
echo "$color, $size, $shape, $prefix_size\n";
?>
The above example will output:
blue, large, sphere, medium
Example 2
$a = $param['a']; $b = $param['b']; $c = $param['c']; $d = $param['d']; $e = $param['e']; $f = $param['f']; $g = $param['g']; $h = $param['h']; $i = $param['i']; $j = $param['j'];
extract($param);
When/When-Not to Use extract() in php
When-Not
Its use is very simple but we have to use it very carefully on untrusted data, like user input (i.e. $_GET, $_FILES, $_REQUEST, etc.). If you do, for example if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting flags
values such as EXTR_SKIP
When
We can usually use it while passing variables/arrays/input params from one function to an other. For more code readability, we can use EXTR_PREFIX_ALL
flag to append a prefix with each param name to identify it uniquely similar to mention in example 1.