Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

extract

Creates variables from the keys of an array.

Syntax

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

Parameters

ParameterTypeDescription
$arrayarrayA "name => value" array.
$flagsintOptional. What to do when a variable already exists.
$prefixstringOptional. 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 250

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.