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
Syntax
extract(array &$array, int $flags = EXTR_OVERWRITE, string $prefix = ""): int
Parameters
| Parameter | Description |
|---|---|
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
<?php
$row = ["title" => "Arrays", "views" => 1200];
extract($row);
echo "$title has $views views\n";
Output
Arrays has 1200 views
<?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)orextract($_POST)lets visitors overwrite any variable. Read the keys you need explicitly.
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
compact()· Create an array from variable names, using each name as the key and the variable's value as the value.- PHP Arrays · tutorial
- PHP Array Functions · tutorial