(PHP 4, PHP 5, PHP 7)
extract — 锟斤拷锟斤拷锟斤拷锟叫斤拷锟斤拷锟斤拷锟斤拷锟诫到锟斤拷前锟侥凤拷锟脚憋拷
&$array
[, int $flags
= EXTR_OVERWRITE
[, string $prefix
= NULL
]] ) : int锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫碉拷锟诫到锟斤拷前锟侥凤拷锟脚憋拷锟叫★拷
锟斤拷锟矫匡拷锟斤拷锟斤拷锟斤拷锟斤拷欠锟斤拷锟斤拷锟斤拷为一锟斤拷锟较凤拷锟侥憋拷锟斤拷锟斤拷锟斤拷同时也锟斤拷锟酵凤拷锟脚憋拷锟斤拷锟斤拷锟叫的憋拷锟斤拷锟斤拷锟侥筹拷突锟斤拷
array
一锟斤拷锟斤拷锟斤拷锟斤拷锟介。锟剿猴拷锟斤拷锟结将锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷为锟斤拷锟斤拷锟斤拷值锟斤拷
锟斤拷每锟斤拷锟斤拷锟斤拷值锟皆讹拷锟斤拷锟节碉拷前锟侥凤拷锟脚憋拷锟叫斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟杰碉拷
flags
锟斤拷 prefix
锟斤拷锟斤拷锟斤拷影锟届。
锟斤拷锟斤拷使锟矫癸拷锟斤拷锟斤拷锟介,锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟介将锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
EXTR_PREFIX_ALL
锟斤拷锟斤拷 EXTR_PREFIX_INVALID
锟斤拷
flags
锟皆达拷锟角凤拷锟斤拷锟斤拷锟街和筹拷突锟侥硷拷锟斤拷锟侥凤拷锟斤拷锟斤拷锟斤拷锟斤拷取锟斤拷锟斤拷锟�
flags
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值之一锟斤拷
EXTR_OVERWRITE
EXTR_SKIP
EXTR_PREFIX_SAME
prefix
锟斤拷
EXTR_PREFIX_ALL
prefix
锟斤拷
EXTR_PREFIX_INVALID
prefix
锟斤拷
EXTR_IF_EXISTS
EXTR_PREFIX_IF_EXISTS
EXTR_REFS
array
锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷锟皆碉拷锟斤拷使锟斤拷锟斤拷锟斤拷锟街撅拷锟斤拷锟斤拷锟�
flags
锟斤拷锟斤拷 OR 锟斤拷锟斤拷锟斤拷锟轿何憋拷志锟斤拷锟绞癸拷谩锟�
锟斤拷锟矫伙拷锟街革拷锟� flags
锟斤拷锟津被假讹拷为 EXTR_OVERWRITE
锟斤拷
prefix
注锟斤拷 prefix
锟斤拷锟斤拷
flags
锟斤拷值锟斤拷
EXTR_PREFIX_SAME
锟斤拷EXTR_PREFIX_ALL
锟斤拷EXTR_PREFIX_INVALID
锟斤拷 EXTR_PREFIX_IF_EXISTS
时锟斤拷要锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷锟角白猴拷锟侥斤拷锟斤拷锟斤拷呛戏锟斤拷谋锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷岬硷拷氲斤拷锟斤拷疟锟斤拷小锟角白猴拷锟斤拷锟斤拷锟斤拷锟斤拷之锟斤拷锟斤拷远锟斤拷锟斤拷锟揭伙拷锟斤拷禄锟斤拷摺锟�
锟斤拷锟截成癸拷锟斤拷锟诫到锟斤拷锟脚憋拷锟叫的憋拷锟斤拷锟斤拷目锟斤拷
Example #1 extract() 锟斤拷锟斤拷
extract() 锟斤拷一锟街匡拷锟斤拷锟矫凤拷锟角斤拷 wddx_deserialize() 锟斤拷锟截的斤拷锟斤拷锟斤拷锟斤拷械锟斤拷锟斤拷莸锟斤拷氲斤拷锟斤拷疟锟斤拷锟斤拷锟斤拷去锟斤拷
<?php
/* 锟劫讹拷 $var_array 锟斤拷 wddx_deserialize 锟斤拷锟截碉拷锟斤拷锟斤拷*/
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
blue, large, sphere, medium
$size 没锟叫憋拷锟斤拷锟角o拷锟斤拷为指锟斤拷锟斤拷
EXTR_PREFIX_SAME
锟斤拷锟斤拷使锟斤拷 $wddx_size
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街革拷锟斤拷锟� EXTR_SKIP
锟斤拷锟斤拷 $wddx_size
也锟斤拷锟结被锟斤拷锟斤拷锟斤拷EXTR_OVERWRITE
锟斤拷使 $size
锟斤拷值为"medium"锟斤拷EXTR_PREFIX_ALL
锟斤拷锟斤拷锟斤拷锟铰憋拷锟斤拷
$wddx_color锟斤拷$wddx_size 锟斤拷
$wddx_shape锟斤拷
锟斤拷要锟皆诧拷锟斤拷锟斤拷锟轿碉拷锟斤拷锟斤拷使锟斤拷
extract()锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷锟斤拷锟诫($_GET锟斤拷 $_FILES锟斤拷...锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿碉拷锟揭拷锟绞憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
register_globals
锟斤拷锟较达拷锟诫,要确锟斤拷使锟矫诧拷锟结覆锟角碉拷
extract_type
值锟斤拷锟斤拷锟斤拷
EXTR_SKIP
锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷应锟矫帮拷锟斤拷 variables_order 锟斤拷 php.ini 锟斤拷
锟斤拷锟斤拷锟剿筹拷锟斤拷锟斤拷锟饺★拷锟�
Note:
If you still have register_globals and it is turned on, if you use extract() on $_FILES and specify
EXTR_SKIP
, you may be surprised at the results.WarningThis is not recommended practice and is only documented here for completeness. The use of register_globals is deprecated and calling extract() on untrusted data such as $_FILES is, as noted above, a potential security risk. If you encounter this issue, it means that you are using at least two poor coding practices.
You might expect to see something like the following:<?php
/* Suppose that $testfile is the name of a file upload input
and that register_globals is turned on. */
var_dump($testfile);
extract($_FILES, EXTR_SKIP);
var_dump($testfile);
var_dump($testfile['tmp_name']);
?>However, you would instead see something like this:string(14) "/tmp/phpgCCPX8" array(5) { ["name"]=> string(10) "somefile.txt" ["type"]=> string(24) "application/octet-stream" ["tmp_name"]=> string(14) "/tmp/phpgCCPX8" ["error"]=> int(0) ["size"]=> int(4208) } string(14) "/tmp/phpgCCPX8"string(14) "/tmp/phpgCCPX8" string(14) "/tmp/phpgCCPX8" string(1) "/"This is due to the fact that since register_globals is turned on, $testfile already exists in the global scope when extract() is called. And since
EXTR_SKIP
is specified, $testfile is not overwritten with the contents of the$_FILES
array so $testfile remains a string. Because strings may be accessed using array syntax and the non-numeric string tmp_name is interpreted as 0, PHP sees $testfile['tmp_name'] as $testfile[0].