0

I have the two following files:

main.php

include("functions.php")  
__EXTRACT();  
echo $testvar;

functions.php

function __EXTRACT(){
extract($_POST, EXTR_SKIP);
}

However, having a form with a textbox called testvar I can't get the extract function to extract the data.. if I remove the function call and insert it the extract statement directly into main.php it works. The include is not a problem as other functions in it works. Any ideas?

Cheers

2 Answers 2

3

You can't do that: in your case extract() creates variables inside the __EXTRACT() function, when the function ends those variables are gone.

Anyway using extract() is very rarely a good idea.

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

4 Comments

Thanks, but is there any way to do it, so it would create the variable outside the function?
Use extract() directly or, better, use values from $_POST without extracting them.
Hehe, I know that $_POST usage is the best, but this is for rapid prototyping, just wanted to know if it was possible. Thanks again.
Extract is very good for small projects or prototyping, or anywhere where you have limited data/few columns in your database. EXTR_PREFIX_ALL used with the record name is excellent to solve variable name conflicts. There's no reason why extract() couldn't have an EXTR_DECLARE GLOBAL flag to allow its use inside functions, in my opinion. In the PHP manual for extract() you will see for which arrays it's dangerous to use - for all others, it's a nice and powerful function.
0

You don't want to do this. This is basically just emulating register_globals inside of PHP. There's a reason it has been off by default for years, and is being removed in 6...

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.