views:

340

answers:

3

Hello,

PHP's extract() function can take on one of several extract_types. But what's the difference between extr_prefix_same and extr_prefix_if_exists? The manual makes it sound like, in either case, new variables will be prefixed if the variable name already exists.

Thanks!

+3  A: 

When using EXTR_PREFIX_IF_EXISTS, if the variable doesn't already exist then the prefixed version won't be created either. In this example:

function test() {
    $a = 12345;

    extract(array('a' => 1, 'b' => 2, 'c' => 3), EXTR_PREFIX_IF_EXISTS, 'my_');

    var_export(get_defined_vars());
}
test();

$my_b and $my_c aren't created because $b and $c don't exist.

too much php
Thanks a bunch! Was reading too quickly and missed the connection to `EXTR_IF_EXISTS`. Makes perfect sense now.Thanks to Alan Storm and jason below, too.
CartoonChess
The wording in the manual isn't clear at all - I had to run some experiments to figure out how it behaves.
too much php
+1  A: 

Based on the manual definitions, EXTR_PREFIX_SAME will create variables based on the key name, and if a variable in the local space already exists, a prefix will be added to the variable name.

By contrast, EXTR_PREFIX_IF_EXISTS would appear to inherit the behavior of EXTR_IF_EXISTS (only overwrite if the variables already exist), but instead of overwriting the local variables, a prefixed version will be created.

Consider the following

$array = Array();
$array['foo'] = 'foo';
$array['bar'] = 'bar';
$array['baz'] = 'baz'; 

$foo = 'local foo';
$bar = 'local bar';

extract($array, EXTR_PREFIX_SAME, 'pre');

print_r(get_defined_vars());

//partial output 
//Array
//(
// [array] => Array
//  (
//   [foo] => foo
//   [bar] => bar
//   [baz] => baz
//  )
//
// [foo] => local foo
// [bar] => local bar
// [pre_foo] => foo
// [pre_bar] => bar
// [baz] => baz
//)

So with EXTR_PREFIX_SAME, the values of $foo and $bar will remain the same, and three new local variables ($pre_foo, $pre_bar, and $baz) will be defined. However if we use EXTR_PREFIX_IF_EXISTS

$array = Array();
$array['foo'] = 'foo';
$array['bar'] = 'bar';
$array['baz'] = 'baz'; 

$foo = 'local foo';
$bar = 'local bar';

extract($array, EXTR_PREFIX_IF_EXISTS, 'pre');

print_r(get_defined_vars());

//partial output 
//Array
//(
// [array] => Array
//  (
//   [foo] => foo
//   [bar] => bar
//   [baz] => baz
//  )
//
// [foo] => local foo
// [bar] => local bar
// [pre_foo] => foo
// [pre_bar] => bar
//)

The values of $foo and $bar are still preserved, but only TWO new variables are imported into the local space. Since $baz isn't a variable that already exists the EXTR_PREFIX_IF_EXISTS tells PHP to ignore the 'baz' key in the array.

Alan Storm
+2  A: 

EXTR_PREFIX_SAME will extract all variables, and only prefix ones that exist in the current scope.

EXTR_PREFIX_IF_EXISTS will only extract variables that exist in the current scope, and prefix them with the desired prefix.

So, for example:

$foo = 'foo';
$bar = 'bar';

extract(array('foo' => 'moo', 'bar' => 'mar', 'baz' => 'maz'), EXTR_PREFIX_IF_EXISTS, 'prefix');

isset($prefix_foo); // true
isset($prefix_baz); // false
isset($baz); // false

While....

$foo = 'foo';
$bar = 'bar';

extract(array('foo' => 'moo', 'bar' => 'mar', 'baz' => 'maz'), EXTR_PREFIX_SAME, 'prefix');

isset($prefix_foo); // true
isset($prefix_baz); // false
isset($baz); // true
jason