PHP extract function tutorial shows how to import variables from an array into the current symbol table. Learn extract with practical examples.
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.
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.
This demonstrates the simplest usage of extract to create variables.
basic_extract.php
<?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.
This example shows how to prefix all extracted variables for safety.
prefix_extract.php
<?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.
Demonstrates how to skip variables that would overwrite existing ones.
skip_extract.php
<?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.
Shows how to extract variables as references to array elements.
refs_extract.php
<?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.
Demonstrates extracting variables from a complex, nested array structure.
complex_extract.php
<?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.
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.
This tutorial covered the PHP extract function with practical examples showing its usage for importing variables from arrays.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.