Jump to Navigation

061 - extract() function

The extract() function converts elements in an array into variables in their own right, an act commonly called "exporting"
in other languages. Extract takes a minimum of one parameter, an array, and returns the number of elements extracted.
The following example is useful for retriving information from database.


$query = "SELECT * FROM country WHERE  location = 'North America'";
$results = mysql_query($query) or die (mysql_error());
$rows = mysql_num_rows($results);
echo $rows;
while ($row = mysql_fetch_array($results)) {
echo $contry_name;
echo $language;
echo $population;
echo "****************"

Possible value for the second parameter to extract():

EXTR_OVERWRITE On collision, overwite the existing variable.
EXTR_SKIP On collision, do not overwite the existing variable.
EXTR_PREFIX_SAME On collision, prefix the variable name with the prefix specified by parameter three.
EXTR_PREFIX_ALL Prefix all variables with the prefix specified by parameter three, whether of not there is a collision.
EXTR_PREFIX_INVALID Use the prefix specified by parameter three only when variable names would otherwise be illegal.
EXTR_IF_EXISTS Set variable only if they already exist.
EXTR_PREFIX_IF_EXISTS Ceate prefixed variables only if non-prefixed version already exists.
EXTRA_REFS Extract variables as reference rather than copied.



Main menu 2

Story | by Dr. Radut