0

I was trying to figure out the behavior of EXTR_PREFIX_IF_EXISTS flag in extract() function. But when I run my code, it adds more than the desired number of variables(two in this case) to current symbol table as indicated by the line:variables added. The output indicates three variables were added instead of two and variable 'key3' exists. Now I can't figure out what's causing this behavior or whether I misunderstood this concept. I'm new to PHP. Any help will be really appreciated.

my code:

$key1 = 'old';
$key2 = 'old';
$my_array = array (
    'key1'  => 'new value1',
    'key2'  => 'new value2',
    'key3'  => 'new value3'
);

$num = extract ($my_array, EXTR_PREFIX_IF_EXISTS, "prefixed");
echo "variables added:$num<br />"; //number of variables imported to symbol table
echo isset($key1) ? 'TRUE': 'FALSE';
echo "<br />";// true
echo isset($prefixed_key3) ? 'TRUE': 'FALSE'; 
echo "<br />";
echo isset($key3) ? 'TRUE': 'FALSE';// should output false

output:

variables added:3
TRUE
FALSE
TRUE

5
  • Try print_r(get_defined_vars()); which will show you what variables are defined (and so which ones you create) Commented Sep 19, 2018 at 6:48
  • I did as Nigel said and it works!. now it displays only two variables were added. thanks Nigel. Commented Sep 19, 2018 at 7:04
  • That's really weird because adding a print_r shouldn't have changed the output... the behaviour you were seeing was definitely incorrect, you should only see that behaviour with the EXTR_PREFIX_SAME flag. This demo on rextester demonstrates the correct behaviour of each flag. Commented Sep 19, 2018 at 7:08
  • @Nick - it shouldn't (and I think your answer is correct, so worth undeleting), the suggestion was more a debugging aid than an answer. Commented Sep 19, 2018 at 7:11
  • @NigelRen actually my answer was wrong, I had the flag behaviour mixed up with EXTR_PREFIX_SAME. One wonders if OP had changed the flag but then not uploaded the new code??? Commented Sep 19, 2018 at 7:13

1 Answer 1

1
 EXTR_PREFIX_IF_EXISTS

means prefix existing variable name when name collisions occur before extracting the variable to the scope and create a new variable if no collision occurs.In your case you didn't define $Key3 anywhere before the extract function so the actual behavior is completely normal...

In your code extract proceed actually this way:

loop through your array and check if the variable already exists. If it exists, according to your flag, create a new variable with the name prefix+_+the existing name . In your case you already defined $key1 and $key2 before the use of extract so 2 variables are created with prefixed name but as already said the variable $key3 is undefined before extract so it creates one more variable without prefix .Thus make the number of variables added correct(3 in your case).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.