(PHP 4, PHP 5, PHP 7)
extract — �������н��������뵽��ǰ�ķ��ű�
&$array
[, int $flags
= EXTR_OVERWRITE
[, string $prefix
= NULL
]] ) : int�����������������������е��뵽��ǰ�ķ��ű��С�
���ÿ���������Ƿ������Ϊһ���Ϸ��ı�������ͬʱҲ���ͷ��ű������еı������ij�ͻ��
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 û�б����ǣ���Ϊָ����
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].