PHP extract()
Published Aug 2, 2023
Contribute to Docs
The extract() method imports variables into the current symbol table from an array.
Syntax
extract($array, flags, prefix)
The extract() function has one required parameter and some optional parameters:
$array: Specifies the input array.flags: Optional. Theextract()function checks for invalid variable names and collisions with existing variable names. This parameter specifies how invalid and colliding names are treated. Below are possible values:EXTR_OVERWRITE: Default. On collision, the existing variable is overwritten.EXTR_SKIP: On collision, the existing variable is not overwritten.EXTR_PREFIX_SAME: On collision, the variable name will be given a prefix.EXTR_PREFIX_ALL: All variable names will be given a prefix.EXTR_PREFIX_INVALID: Only invalid or numeric variable names will be given a prefix.EXTR_IF_EXISTS: Only overwrites existing variables in the current symbol table, otherwise does nothing.EXTR_PREFIX_IF_EXISTS: Only adds prefix to variables if the same variable exists in the current symbol table.EXTR_REFS: Extracts variables as references.
prefix: Optional. IfEXTR_PREFIX_SAME,EXTR_PREFIX_ALL,EXTR_PREFIX_INVALIDorEXTR_PREFIX_IF_EXISTSare used in theflagsparameter, a specified prefix is required.
The extract() function returns the number of variables extracted on success.
Example
The following example demonstrates an implementation of the extract() function:
<?php$a = "Original";$my_array = array("a" => "Rat","b" => "Mule", "c" => "Ass");extract($my_array);echo "\$a = $a; \$b = $b; \$c = $c";?>
The example will result in the following output:
$a = Rat; $b = Mule; $c = Ass
Codebyte Example
This example is runnable and uses the extract() function:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours