PHP extract() Function: Importing Variables from an Array into the Symbol Table
This article explains how PHP's extract() function imports variables from an associative array into the current symbol table, details its parameters, conflict‑resolution flags, return values, and provides a complete example with code and expected output.
The PHP extract() function imports variables from an associative array into the current symbol table, using the array keys as variable names and the corresponding values as variable values.
It checks each key for validity as a variable name and handles conflicts according to the $extract_type flag, which can be EXTR_OVERWRITE , EXTR_SKIP , EXTR_PREFIX_SAME , EXTR_PREFIX_ALL , EXTR_PREFIX_INVALID , EXTR_IF_EXISTS , EXTR_PREFIX_IF_EXISTS , or EXTR_REFS . If no flag is supplied, EXTR_OVERWRITE is assumed.
The optional $prefix parameter is used with the prefix‑related flags to prepend a string (and an underscore) to variable names, and it is ignored for other flags.
When successful, extract() returns the number of variables extracted; it returns FALSE for an empty array.
Example:
"blue",
"size" => "medium",
"shape" => "sphere"
);
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>The script outputs:
blue, large, sphere, mediumLaravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.