Using the extract() Function





7
Date Submitted Mon. Nov. 28th, 2005 4:35 PM
Revision 1 of 1
Beginner Michael
Tags Extract | PHP
Comments 3 comments
Using the extract() Functio
<?php
// Say that we just submitted a POST form that had 5 fields, from test1 to test5. We // could put all the contents of $_POST into variables individually, like this.

$test1 = $_POST["test1"];
$test2 = $_POST["test2"];
$test3 = $_POST["test3"];
$test4 = $_POST["test4"];
$test5 = $_POST["test5"];

// But extract() will let you do all of the above in a much easier way.
extract($_POST);

// You now have $test1, $test2, $test3, $test4, and $test5 by writing that one line. // However, say you have a different variable named $test4 and don't want to delete // it. This is where the optional arguements of extract() come in.
extract($_POST, EXTR_PREFIX_ALL, "f_");

// The above function will make $f_test1, $f_test2, $f_test3, $f_test4, and $f_test5.
// There are a bunch of other choices other than EXTR_PREFIX_ALL, like
// EXTR_PREFIX_SAME, which only prefixes variables that already exist.

Michael Boutros

www.michaelboutros.com

Comments

Comments Carefull with this
Mon. Sep. 11th, 2006 7:34 AM    Scripter ctiggerf
Comments Just like register globals...
Thu. Feb. 16th, 2006 7:15 PM    Helper mercutio
  Comments so very true
Mon. Aug. 7th, 2006 5:04 PM    Scripter bertheymans

Voting