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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read