extract
Creates variables from the keys of an array.
Syntax
extract(array &$array, int $flags = EXTR_OVERWRITE, string $prefix = ""): intParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | A "name => value" array. |
| $flags | int | Optional. What to do when a variable already exists. |
| $prefix | string | Optional. A prefix for the variable names. |
Return value
intThe number of variables created.
Examples
Keys become variables:
<?php
extract(['title' => 'Tea', 'price' => 250]);
echo $title, ' ', $price;Output:
Tea 250Notes
Never call extract() on user data — $_GET, $_POST: a visitor could overwrite any of your variables. Destructuring is clearer and safer: ['title' => $title] = $data.
See also
- compact — Gathers variables into an array by their names.
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.