PHP extract() Function Example

PHP Script:


<?php
$a = 'Carrot';
$sample_array = array("a" => "Apple","b" => "Banana", "c" => "Grapes");
extract($sample_array);
// Extraction Rule = EXTR_OVERWRITE [Default]
echo 'EXTR_OVERWRITE: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_SKIP);
// Extraction Rule = EXTR_SKIP
echo 'EXTR_SKIP: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_PREFIX_SAME,'copy1');
// Extraction Rule = EXTR_PREFIX_SAME
echo 'EXTR_PREFIX_SAME: $a = '.$a.' $b = '.$b.' $c = '.$c.' $copy1_a = '.$copy1_a;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_PREFIX_ALL,'copy2');
// Extraction Rule = EXTR_PREFIX_ALL
echo 'EXTR_PREFIX_ALL: $copy2_a = '.$copy2_a.' $copy2_b = '.$copy2_b.' $copy2_c = '.$copy2_c;
echo "< br/ >";

$a = 'Carrot';
$sample_array2 = array("1" => "Apple","b" => "Banana", "c" => "Grapes");
extract($sample_array2,EXTR_PREFIX_INVALID,'copy3');
// Extraction Rule = EXTR_PREFIX_INVALID
echo 'EXTR_PREFIX_INVALID: $copy3_1 = '.$copy3_1.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
$sample_array3 = array("a" => "Cherry","b" => "Banana", "c" => "Grapes");
extract($sample_array3,EXTR_IF_EXISTS);
// Extraction Rule = EXTR_IF_EXISTS
echo 'EXTR_IF_EXISTS: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array3,EXTR_PREFIX_IF_EXISTS,'copy4');
// Extraction Rule = EXTR_PREFIX_IF_EXISTS
echo 'EXTR_PREFIX_IF_EXISTS: $copy4_a = '.$copy4_a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_REFS);
// Extraction Rule = EXTR_REFS
echo 'EXTR_REFS: $a = '.$a.' $b = '.$b.' $c = '.$c;
?>

Output:


EXTR_OVERWRITE: $a = Apple $b = Banana $c = Grapes
EXTR_SKIP: $a = Carrot $b = Banana $c = Grapes
EXTR_PREFIX_SAME: $a = Carrot $b = Banana $c = Grapes $copy1_a = Apple
EXTR_PREFIX_ALL: $copy2_a = Apple $copy2_b = Banana $copy2_c = Grapes
EXTR_PREFIX_INVALID: $copy3_1 = Apple $b = Banana $c = Grapes
EXTR_IF_EXISTS: $a = Cherry $b = Banana $c = Grapes
EXTR_PREFIX_IF_EXISTS: $copy4_a = Cherry $b = Banana $c = Grapes
EXTR_REFS: $a = Apple $b = Banana $c = Grapes

Description:

PHP extract() function is used to import variables into the local symbol table from an array.

PHP extract() function parameters:

1- Array (Required)
2- Rules for extracting variables (Optional):
– This parameter tells how to treat with invalid and colliding names.
– Possible parametric values:
EXTR_OVERWRITE – Default. If names are collide existing variable is overwritten as shown in above example $a = 'Carrot' is overwrite by $a = 'Apple'.
EXTR_SKIP – If names are collide existing variable is not overwritten as shown in above example.
EXTR_PREFIX_SAME – If names are collide existing variable will be given a prefix as shown in above example.
EXTR_PREFIX_ALL – All variable names will be given a prefix as shown in above example.
EXTR_PREFIX_INVALID – Only invalid or numeric variable names will be given a prefix as shown in above example.
EXTR_IF_EXISTS – Only overwrite existing variables in the current symbol table, otherwise do nothing as shown in above example.
EXTR_PREFIX_IF_EXISTS – Only add prefix to variables if the same variable exists in the current symbol table as shown in above example.
EXTR_REFS – Extracts variables as references. The imported variables are still referencing the values of the array parameter as shown in above example.
3- Prefix (Optional):
– This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character as shown in above example.
– It is required with following extract rules:
[EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s