PHP 8 reference · Array Functions

PHP extract() Function

Create variables from an array's keys and values in the current scope. Never use it on user input.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
array The name => value pairs.
flags optional How to handle existing variables, such as EXTR_SKIP or EXTR_PREFIX_ALL. Default: EXTR_OVERWRITE
prefix optional A prefix for the variable names, used with prefix flags. Default: ""

Return value

The number of variables created.

Examples

Array to variablesTry it yourself
<?php
$row = ["title" => "Arrays", "views" => 1200];
extract($row);
echo "$title has $views views\n";

Output

Arrays has 1200 views
Don't overwrite existing variablesTry it yourself
<?php
$title = "kept";
$data = ["title" => "new", "author" => "Ada"];
echo extract($data, EXTR_SKIP), "\n";
echo "$title by $author\n";

Output

1
kept by Ada

Common mistakes

  • extract($_GET) or extract($_POST) lets visitors overwrite any variable. Read the keys you need explicitly.

Version history

VersionChange
PHP 4.0Added

Was this page helpful?