为什么要在PHP中使用 extract()函数?
Extract()函数是PHP中的一个内置函数,用于在变量名冲突时使用。当某人编写大约 100 行的代码时,有可能他已经声明了变量,然后我们分配的变量值之间存在冲突。通过使用 extract()函数,我们可以选择要保存的值。
此外,我们可以使用它将数组的变量提取到符号表中。它提取不同符号中的键值对,我们可以通过调用它们的变量直接访问它们。永远记住,它不能用于用户给定的数据,我们可以从一些 get 或 post 或任何其他方法获取数据。
extract(array, extract_rules, prefix)
- 数组:我们可以传递要提取的数组。
- extract_rules:有 8 种规则执行不同类型的事情
- EXTR_OVERWRITE
- EXTR_SKIP
- EXTR_IF_EXISTS
- EXTR_REFS
- EXTR_PREFIX_SAME
- EXTR_PREFIX_INVALID
- EXTR_PREFIX_IF_EXISTS
- EXTR_PREFIX_ALL
- 前缀:这通过前缀和下划线声明变量。仅在规则为 EXTR_PREFIX_SAME、EXTR_PREFIX_ALL、EXTR_PREFIX_INVALID 或 EXTR_PREFIX_IF_EXISTS 时使用
通过给定的例子,我们可以一一理解每一条规则。
示例 1:在此,我们只是传递数组并且没有提取规则。所以它不能对 $a 做任何事情并且它保持不变。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c ";
?>
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_OVERWRITE);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_SKIP);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_IF_EXISTS);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_REFS);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_SAME,"gfg");
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
echo "gfg_a = $gfg_a "."
";
?>
PHP
";
echo "gfg_1 = $gfg_1 "."
";
echo "gfg_2 = $gfg_2 "."
";
?>
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_IF_EXISTS,"gfg");
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
echo "gfg_a = $gfg_a "."
";
?>
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_ALL,"gfg");
echo "a = $a "."
";
echo "gfg_a = $gfg_a "."
";
echo "gfg_b = $gfg_b "."
";
echo "gfg_c = $gfg_c "."
";
?>
输出:
示例 2:在此,我们使用EXTR_OVERWRITE,因此它会覆盖变量值。此外,没有如上所述使用的前缀。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_OVERWRITE);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
输出:
示例 3:在此,我们使用EXTR_SKIP,因此它在分配变量后跳过该值,您可以看到 $a 值保留了它的值并打印“GeeksforGeeks”。如上所述,没有使用前缀。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_SKIP);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
输出:
示例 4:在此,我们使用EXTR_IF_EXISTS,因此它保留了已经声明的变量,否则对不匹配的变量不做任何事情。正如您在下图中看到的,有未定义的变量 b,c 因为它尚未声明,所以它们没有被提取。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_IF_EXISTS);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
输出:
例5:这里我们使用EXTR_REFS,所以提取后引用的是同一个变量。如您所见,您可以仅通过其值名称访问所有变量,如下图所示。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_REFS);
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
?>
输出:
示例 6:在此,我们使用EXTR_PREFIX_SAME。在这种情况下,当变量已经声明时,它会保留其值,并创建带有前缀和下划线变量的新变量,然后将提取的值分配给它,如下图所示。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_SAME,"gfg");
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
echo "gfg_a = $gfg_a "."
";
?>
输出:
示例 7:在此,我们使用EXTR_PREFIX_INVALID。有一个带有数字键的数组,当我们提取值时,它会创建带有前缀名称的变量并分配值,如下图所示。
PHP
";
echo "gfg_1 = $gfg_1 "."
";
echo "gfg_2 = $gfg_2 "."
";
?>
输出:
示例 8:在此,我们使用EXTR_PREFIX_IF_EXISTS,如果变量已被声明,则它保留其值并为新变量分配前缀并分配提取值,如下图所示。如果存在相同变量的非前缀版本,它只会创建前缀变量。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_IF_EXISTS,"gfg");
echo "a = $a "."
";
echo "b = $b "."
";
echo "c = $c "."
";
echo "gfg_a = $gfg_a "."
";
?>
输出:
示例 9:在此,我们使用EXTR_PREFIX_ALL,因此它提取所有具有前缀名称的变量并赋值,原始变量保持不变。
PHP
"Geeks","b" => "for", "c" => "Geeks");
extract($my_array, EXTR_PREFIX_ALL,"gfg");
echo "a = $a "."
";
echo "gfg_a = $gfg_a "."
";
echo "gfg_b = $gfg_b "."
";
echo "gfg_c = $gfg_c "."
";
?>
输出: