W3docs

extract()

array_extract() is a built-in function in PHP that allows you to extract values from an array and create variables with those values. This function takes two

What is extract() in PHP?

extract() is a built-in PHP function that imports variables from an array into the current symbol table. It takes an array as its first argument and optionally accepts a second argument for flags and a third for a prefix. The function returns the number of variables successfully extracted.

How to use extract() in PHP

To use the extract() function, you first need an array that contains the values you want to extract. Here's an example array:

$person = array(
    'name' => 'John Doe',
    'age' => 30,
    'email' => '[email protected]'
);

By default, extract() creates variables for all keys in the array. If you only want to extract specific keys, you can combine it with array_intersect_key() and array_flip():

extract(array_intersect_key($person, array_flip(array('name', 'email'))));

After running this code, you will have two variables, $name and $email, that contain the corresponding values from the $person array.

Flags and behavior

The second parameter of extract() controls how existing variables are handled during extraction. Common flags include:

  • EXTR_OVERWRITE (default): Overwrites existing variables with the same name.
  • EXTR_SKIP: Skips extraction if a variable with the same name already exists.
  • EXTR_PREFIX_ALL: Prefixes all new variable names with a specified string.
  • EXTR_REFS: Extracts variables as references.

Security warning

Because extract() creates variables dynamically, it can accidentally overwrite existing variables or superglobals (like $_GET, $_POST, or $_SESSION). This is a common source of security vulnerabilities. Always use EXTR_SKIP or explicitly check for variable existence when extracting data from untrusted sources.

Advantages of using extract()

The extract() function can be a useful tool in PHP because it allows you to quickly and easily create variables from values in an array. This can make your code more readable and easier to maintain.

One advantage of using extract() is that it can save you time and reduce the amount of code you need to write. Instead of manually assigning values from an array to variables, you can use extract() to do it automatically.

Another advantage of using extract() is that it can make your code more readable. By creating variables with descriptive names that correspond to the values in your array, you can make your code easier to understand and more self-documenting.

Conclusion

In conclusion, extract() is a powerful tool in PHP that can make your code more efficient and readable. By allowing you to extract values from an array and create variables with those values, extract() can save you time and reduce the amount of code you need to write. We hope this guide has been helpful in understanding how to use extract() and how it can benefit your PHP code.

Practice

Practice

What is the functionality of the extract() function in PHP?