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:
<?php
/* assume $var_array is the array returned by wddx_deserialize */
$size = "large";
$var_array = array(
"color" => "blue",
"size" => "medium",
"shape" => "sphere"
);
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size
";
?>The script outputs:
blue, large, sphere, mediumSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel 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.
