tags:

views:

309

answers:

6

I am building a function that will validate the values in a form. How can I extract the actual name in string form from a $_POST variable? Ex: $_POST['first_name'] = 'Joe';

I now want to grab the string 'first_name' and use it in a function to declare a variable by that name. I know I can use a foreach loop but isn't there an easier way to do this?

+6  A: 
foreach( $_POST as $key => $value ) {
     $$key = $value;
}

The main difference between using a loop as shown above versus using extract( $_POST ) is that extract puts the variables into global scope, whereas this code will put them in the current scope, thereby making it safer to use than extract

Jacob Relkin
I would suggest useing the `${$key}` format but only to improve readability. Thats entirely a personal preference though.
Darrell Brogdon
+2  A: 

I'm not sure whether this is what you want, but you can use variable variables like so:

$fieldname = "first_name";

$fieldvalue = $_POST[$fieldname];

$$fieldname = $fieldvalue;   // Creates a variable named "first_name"

echo $first_name;  // Outputs the POST variable
Pekka
Not actually.. I just wanted to use the text 'first_name' as the value of a new variable. So if I echoed $fn it would echo 'first_name'
Roger
+1  A: 

I wouldn't use this:

foreach( $_POST as $key => $value ) {
     $$key = $value;
}

...it opens your code to the possibility of someone maliciously setting vars within your program via the form. (i.e., effectively setting globals_register on)

But to answer your core question: your only knowledge of the posted variables is what's in the $_POST array; your only way of finding out what the variable names are is looping through the array keys.

spinn
a) it's what he asked for, and b) it's `register_globals` ;)
Jacob Relkin
Er, yeah, register_globals. And you're right--actually let me refine that to: make sure you don't do that in global scope, and only inside a function. (which is also what you said, but it deserves emphasis)
spinn
I agree, globals are really really bad. But it's what the doctor ordered.
Jacob Relkin
Thanks for that. I'll try it
Roger
Well, if I clean the variable prior to assignment then shouldn't that be ok?
Roger
No, because the issue is someone setting variables that you don't intend to be set. (though I see the "extract" answer pretty much answers your question...lookup register_globals if you're curious about the security issue.)
spinn
+4  A: 

How about

extract($_POST);
newacct
This is the perfect solution; there isn't any reason to loop through all the array values, when there is `extract()` that does all the work.
kiamlaluno
+1  A: 

As far as I understood, you are looking for array_keys($_POST).

ntd
A: 

If you want to execute function based on the key name, your best shot would be something like this:

foreach ($_POST AS $key => $value)
{
    call_user_func("example_func_$key", $value);
}

Note the use of $key within the double-quotes of the first parameter of call_user_func. This will allow you to execute a "dynamically" named-function.

Dominic Barnes