Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Array Functions

PHP compact() and extract() functions

Reference for PHP compact() and extract(): turn variables into an array and an array back into variables, flags, prefixes, and why extract() is risky.

compact() builds an array from variables: you give it the names of variables, and it returns an array mapping each name to the variable's value. extract() does the reverse: it takes an array and creates one variable in the current scope for every key.

They are the two halves of a convenient but easily abused pair. compact() is safe and common in template code. extract() can overwrite variables, so use it only on data you control, and never on $_GET, $_POST or other user input.

PHP
<?php
$name = "Ann";
$age = 31;
$data = compact("name", "age");   // ["name" => "Ann", "age" => 31]

Try it

PHP compact(): variables to an array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP extract(): array to variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
compact(array|string $var_name, array|string ...$var_names): array
extract(array &$array, int $flags = EXTR_OVERWRITE, string $prefix = ""): int
Name Description
$var_name, ...$var_names Names of variables (strings), or arrays of names. Names are given without the $.
$array The array to extract. Passed by reference only for EXTR_REFS.
$flags How to treat name collisions: EXTR_OVERWRITE (default), EXTR_SKIP, EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, EXTR_IF_EXISTS, EXTR_PREFIX_IF_EXISTS; combine with EXTR_REFS using `
$prefix Prefix (followed by _) used by the prefix flags.

Return value: compact() returns the array. extract() returns the number of variables created or changed.

Warnings: compact() warns about undefined variables (Undefined variable $x) and skips them.

Usage notes

  • Security: extract($_POST) lets a visitor create or overwrite any variable, including ones you rely on. Use EXTR_SKIP or a prefix at the very least, or better, read the keys you need explicitly.
  • Scope: both work on the current scope, so inside a function they see the function's variables.- Only string keys are used. Integer keys are ignored unless you use EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID.
  • Static analysis suffers: IDEs and tools cannot tell which variables extract() created. Prefer explicit assignments or list() destructuring (["id" => $id] = $row;).
  • compact() is handy for passing data to a template, or for building a result array at the end of a function.
  • EXTR_REFS makes the new variables references to the array elements, so changing the variable changes the array.