PHP extract() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, extract() turns the keys of an array into variables. Each key becomes a variable name and each value becomes that variable's value.

If you have an array like ["name" => "Asha"], calling extract() creates a variable named $name that holds "Asha". It saves typing when you have many keys, but it can also overwrite variables you already have, so it must be used carefully.

Syntax

Call extract() with the values you need. Start with this line.

PHP
$count = extract($user);
CallWhat it does
extract($user)Creates one variable per valid key and returns how many were created.
extract($data, EXTR_SKIP)Skips any key whose variable already exists, so nothing is overwritten.
extract($row, EXTR_PREFIX_ALL, "post")Creates variables like $post_id and $post_title by adding a prefix.

Only keys that are valid variable names are turned into variables. A key like "first name" or "1abc" is skipped unless a prefix makes it valid. The return value is the number of variables that were set.

Library declarations
PHP
extract(array &$array, int $flags = EXTR_OVERWRITE, string $prefix = ""): int

Parameters

ParameterDescription
arrayRequired. The array whose keys and values become variables.
flagsOptional. Controls what happens when a variable already exists. Common values are EXTR_OVERWRITE (default), EXTR_SKIP, EXTR_PREFIX_SAME and EXTR_PREFIX_ALL.
prefixOptional. Text added before each variable name, joined with an underscore. It is needed for the EXTR_PREFIX_* flags.

Return Value

Return ValueDescription
intThe number of variables that were successfully imported into the current scope.

PHP Examples

Example 1 — Turn Array Keys into Variables

This example takes a small array of user data and creates one variable for each key.

PHP
<?php
$user = ["name" => "Asha", "city" => "Pune"];

$count = extract($user);

echo $name, PHP_EOL;
echo $city, PHP_EOL;
echo "Variables created: ", $count, PHP_EOL;

How It Works

  1. $user has two keys, name and city.
  2. extract() creates the variables $name and $city from those keys.
  3. The variables can now be used directly, so echo prints Asha and Pune.
  4. extract() returned 2 because two variables were created.

Example 2 — Protect Existing Variables with EXTR_SKIP

By default extract() replaces a variable that already exists. EXTR_SKIP keeps the old value instead.

PHP
<?php
$name = "Original";
$data = ["name" => "Changed"];

extract($data, EXTR_SKIP);
echo $name, PHP_EOL;

extract($data);
echo $name, PHP_EOL;

How It Works

  1. $name already holds "Original" before extract() is called.
  2. With EXTR_SKIP the existing variable is left alone, so $name is still "Original".
  3. The second call uses the default flag, EXTR_OVERWRITE.
  4. Now $name is replaced with "Changed". This is the overwrite risk you need to remember.

Example 3 — Add a Prefix to Avoid Name Clashes

A prefix keeps imported variables separate from your own, which is much safer for data from outside your script.

PHP
<?php
$row = ["id" => 5, "title" => "Hello"];

$count = extract($row, EXTR_PREFIX_ALL, "post");

echo "Imported: ", $count, PHP_EOL;
echo $post_id, PHP_EOL;
echo $post_title, PHP_EOL;

How It Works

  1. $row has the keys id and title.
  2. EXTR_PREFIX_ALL adds the prefix "post" and an underscore to every key.
  3. The variables created are $post_id and $post_title, so no other variable can be overwritten.
  4. The count of 2 and both values are printed.

Frequently Asked Questions

It creates variables from an array. Every key that is a valid variable name becomes a variable, and the matching value is assigned to it. It returns the number of variables created.
Only with data you control. With the default flags it overwrites existing variables, so using it on $_GET or $_POST can let a visitor replace variables in your code. Use EXTR_SKIP or a prefix if you need it.
compact(). It takes variable names and builds an array from them, so compact("name", "city") returns ["name" => ..., "city" => ...].
The key was probably not a valid variable name. Names with spaces, dashes or a leading digit are skipped. A prefix such as EXTR_PREFIX_ALL can make some of them valid.
Often yes. Reading values with $user["name"], or using list() and [] destructuring, makes it clear where each variable comes from and cannot overwrite anything by accident.

Next: in_array()

in_array() checks whether a value exists inside an array and returns true or false.

in_array() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful