Why to use extract() function in PHP ?
Last Updated :
10 Mar, 2022
Extract() function is an inbuilt function in PHP which is used when there is conflict in the variable name. When someone writes a code of approximately 100 lines, there is a chance that he declares the variable already then there is a conflict between what value the variable we assign. By using the extract() function we can choose which value to be saved.
Also, we can use it to extract the variable of an array into the symbol table. It extracts key-value pairs in different symbols and we can access them directly by calling their variable. Always remember that it can not be used for the user-given data where we can take data from some get or post or any other methods.
extract(array, extract_rules, prefix)
- array: We can pass the array in which we want to extract.
- extract_rules: There are 8 types of rules which perform different types of things
- EXTR_OVERWRITE
- EXTR_SKIP
- EXTR_IF_EXISTS
- EXTR_REFS
- EXTR_PREFIX_SAME
- EXTR_PREFIX_INVALID
- EXTR_PREFIX_IF_EXISTS
- EXTR_PREFIX_ALL
- prefix: This declares the variable by a prefix along with underscore. It is only used when rules are EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS
We can understand every rule one by one by the given examples.
Example 1: In this, we simply pass the array and there are no extract rules. So it can do nothing with $a and it remains unchanged.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array);
echo "a = $a "."<br>";
echo "b = $b "."<br>";
echo "c = $c ";
?>
Output:
Example 2: In this, we use EXTR_OVERWRITE, so it overwrites the variable value. Also, there is no prefix used as mentioned above.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_OVERWRITE);
echo "a = $a "."<br>";
echo "b = $b "."<br>";
echo "c = $c "."<br>";
?>
Output:
Example 3: In this, we use EXTR_SKIP, so it skips the value after assigning the variable as you can see that $a value retained its value and print "GeeksforGeeks". There is no prefix used as mentioned above.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_SKIP);
echo "a = $a "."<br>";
echo "b = $b "."<br>";
echo "c = $c "."<br>";
?>
Output:
Example 4: In this, we use EXTR_IF_EXISTS, so it retained the variable which is already declared and otherwise does nothing to a non-matched variable. As you can see in the below image there is undefined variable b,c because it is not already declared, so they are not extracted.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_IF_EXISTS);
echo "a = $a "."<br>";
echo "b = $b "."<br>";
echo "c = $c "."<br>";
?>
Output:
Example 5: In this, we use EXTR_REFS, so it refers to the same variable after extracting it. As you can see, you can access all the variables just by their value name as shown in the below image.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_REFS);
echo "a = $a "."<br>";
echo "b = $b "."<br>";
echo "c = $c "."<br>";
?>
Output:
Example 6: In this, we use EXTR_PREFIX_SAME. In this when the variable is already declared then it retained its value and it creates the new variable with prefix along with underscore variable and then assigns the extracted value to it as shown in the below image.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_SAME,"gfg");
echo "a = $a "."<br>";
echo "b = $b "."<br>";
echo "c = $c "."<br>";
echo "gfg_a = $gfg_a "."<br>";
?>
Output:
Example 7: In this, we use EXTR_PREFIX_INVALID. There is an array with numerical keys and when we extract the value it creates variables with prefix name and assigns the values as shown below image.
PHP
<?php
$my_array = array("Geeks","for", "Geeks");
extract($my_array, EXTR_PREFIX_INVALID,"gfg");
echo "gfg_0 = $gfg_0 "."<br>";
echo "gfg_1 = $gfg_1 "."<br>";
echo "gfg_2 = $gfg_2 "."<br>";
?>
Output:
Example 8: In this, we use EXTR_PREFIX_IF_EXISTS, if the variable is already declared then it retained its value and assign the new variable with prefix and assign extract value as shown in the below image. It only creates a prefix variable if a non-prefixed version of the same variable exists.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_IF_EXISTS,"gfg");
echo "a = $a "."<br>";
echo "b = $b "."<br>";
echo "c = $c "."<br>";
echo "gfg_a = $gfg_a "."<br>";
?>
Output:
Example 9: In this, we use EXTR_PREFIX_ALL, so it extracts all variables with prefix names and assigns the value and the original variable remains unchanged.
PHP
<?php
$a = "GeeksforGeeks";
$my_array = array("a" => "Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_ALL,"gfg");
echo "a = $a "."<br>";
echo "gfg_a = $gfg_a "."<br>";
echo "gfg_b = $gfg_b "."<br>";
echo "gfg_c = $gfg_c "."<br>";
?>
Output:
Similar Reads
PHP extract() Function
The extract() Function is an inbuilt function in PHP. The extract() function does array to variable conversion. That is it converts array keys into variable names and array values into variable value. In other words, we can say that the extract() function imports variables from an array to the symbo
3 min read
PHP SplHeap extract() Function
The SplHeap::extract() function is an inbuilt function in PHP which is used to extract a node from top of the heap and sift up. Generally, the Heap Data Structure are of two types: Max-Heap: In a Max-Heap, the key present at the root node must be greatest among the keys present at all of its childre
1 min read
PHP SplPriorityQueue extract() Function
The SplPriorityQueue::extract() function is an inbuilt function in PHP which is used to extract a node from top of the heap and sift up. Syntax: mixed SplPriorityQueue::extract() Parameters: This function does not accept any parameter. Return Value: This function returns the value/priority (or both)
1 min read
PHP mb_ereg_search_pos() Function
The mb_ereg_search_pos() is an inbuilt function in PHP that is used in regular expressions to match a given string. It searches for the first occurrence of a pattern in the string and returns the starting position and the ending position of the match. Syntax:mb_ereg_search_pos( ?string $pattern = nu
2 min read
What is the use of array_count_values() function in PHP ?
In this article, we will discuss array_count_values() function & its usage in PHP, along with understanding its basic implementation through the illustrations. The array_count_values() function is used to count all the values inside an array. In other words, we can say that array_count_values()
2 min read
PHP ZipArchive extractTo() Function
The ZipArchive::extractTo() function is an inbuilt function in PHP that is used to extract the zip archive content in a folder.Syntax:bool ZipArchive::extractTo( string $pathto, array|string|null $files = null)Parameters: This function accepts two parameters that are described below:$pathto: This pa
2 min read
PHP mb_ereg_search_regs() Function
The mb_ereg_search_regs() function is an inbuilt function in PHP that is used for regular expressions to match a given string. If the match is found, then it will return the matched part as an array. Syntax: mb_ereg_search_regs( ?string $pattern = null, ?string $options = null): array|falseParameter
2 min read
PHP token_get_all() Function
The token_get_all() function is an inbuilt function in PHP that is used to tokenize a given PHP source code string into an array of tokens. This function is particularly useful for analyzing, parsing, or manipulating PHP code programmatically. Syntax: token_get_all(string $code, int $flags = 0)Param
2 min read
PHP | var_export() Function
The var_export() is a built-in function in PHP which is used to return the structured value(information) of a variable that is passed to this function as a parameter. This function is similar to the var_dump() function.Syntax: var_export($var, $return) Parameters: This function accepts two parameter
1 min read
PHP | DsVector toArray() Function
The Ds\Vector::toArray() function is an inbuilt function in PHP which is used to convert the vector into an array. All the elements of the vector are copied to the array.Syntax: array public Ds\Vector::toArray( void ) Parameters: This function does not accepts any parameters.Return Value: This funct
2 min read