PHP extract Function
last modified March 13, 2025
The PHP extract
function imports variables from an array into
the current symbol table. It's useful for quickly creating variables from
array keys and values.
Basic Definition
The extract
function takes an associative array and creates
variables for each key-value pair. The variable names match the array keys.
Syntax: extract(array $array, int $flags = EXTR_OVERWRITE, string $prefix = ""): int
.
The function returns the number of variables successfully imported.
Basic extract Example
This demonstrates the simplest usage of extract
to create variables.
<?php $userData = [ 'username' => 'johndoe', 'email' => 'john@example.com', 'age' => 30 ]; extract($userData); echo $username; echo $email; echo $age;
This code creates three variables from the array keys. Each variable holds the corresponding value from the array. The variables are now available in the current scope.
Using EXTR_PREFIX_ALL Flag
This example shows how to prefix all extracted variables for safety.
<?php $config = [ 'host' => 'localhost', 'port' => 3306, 'dbname' => 'testdb' ]; extract($config, EXTR_PREFIX_ALL, 'db'); echo $db_host; echo $db_port; echo $db_dbname;
The EXTR_PREFIX_ALL
flag adds the specified prefix to all
variable names. This prevents naming collisions with existing variables.
Handling Collisions with EXTR_SKIP
Demonstrates how to skip variables that would overwrite existing ones.
<?php $existingVar = 'original value'; $data = [ 'existingVar' => 'new value', 'newVar' => 'some data' ]; extract($data, EXTR_SKIP); echo $existingVar; echo $newVar;
With EXTR_SKIP
, the function skips keys that match existing
variable names. The original value of $existingVar
is preserved.
Extracting with EXTR_REFS
Shows how to extract variables as references to array elements.
<?php $originalArray = [ 'color' => 'blue', 'size' => 'medium' ]; extract($originalArray, EXTR_REFS); $color = 'red'; // Modifies the original array print_r($originalArray);
Using EXTR_REFS
creates variables that reference the original
array elements. Changing these variables modifies the original array.
Complex Array Extraction
Demonstrates extracting variables from a complex, nested array structure.
<?php $userProfile = [ 'personal' => [ 'name' => 'Alice', 'birthdate' => '1990-05-15' ], 'account' => [ 'username' => 'alice123', 'last_login' => '2023-04-01' ] ]; extract($userProfile['personal']); extract($userProfile['account'], EXTR_PREFIX_ALL, 'acc'); echo $name; echo $birthdate; echo $acc_username; echo $acc_last_login;
This shows extracting from nested arrays. We extract personal data directly and prefix account data to avoid naming conflicts between the two sections.
Best Practices
- Security: Never use extract with untrusted input (like $_GET).
- Prefixing: Use prefixes to avoid variable collisions.
- Scope: Be aware extract creates variables in current scope.
- Readability: Consider alternatives when code becomes unclear.
Source
This tutorial covered the PHP extract
function with practical
examples showing its usage for importing variables from arrays.
Author
List all PHP Array Functions.