PHP extract()

Posted by & filed under PHP.

You shouldn’t use this function on untrusted data, but if you clean it up – and know what the expected variables will be called – this is a handy function.

Here an example $_POST:

$_POST = array(
    'firstname' => 'Mark',
    'lastname' => 'Flint'

First clean the input:

$clean = create_function('$v', 'return trim(strip_tags(stripslashes($v)));');
$post = array_map($clean, $_POST);

Now extract the key-value pairs into varname-values:

echo $firstname . ' ' . $lastname; // Mark Flint

There’s a pile of constants that control behaviour in case of scope clashes etc.. see

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>