426

professional dilettante

Using PHP Variable Variables With Form Data

A nice technique I discovered last week, mostly because I am lazy. Typically when handling form data with PHP, you need to create variables one by one, plucking values out of the PHP superglobal associative array, $_POST. For each form field whose value you want to validate and store, we usually declare variables and assign [...]

A nice technique I discovered last week, mostly because I am lazy. Typically when handling form data with PHP, you need to create variables one by one, plucking values out of the PHP superglobal associative array, $_POST. For each form field whose value you want to validate and store, we usually declare variables and assign values. The value we want is stored inside a named index in $_POST. Say for example we have a form with two fields: firstname, and address.

<input name="firstname" type="text" id="firstname" />
<input name="address" type="text" id="address" />

We might do something like:

$firstname = $_POST['firstname'];
$address = $_POST['address'];

And we would do that for each item from the form that we need. Instead of doing that, we can use PHP’s variable variables to automagically create variables. The variable names (important so we can refer to them later) will correspond to the name attributes on the form input fields. So we can do this:

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

The above foreach loop, while probably cryptic and not good for a person who is inheriting the code, is only three lines of code. And after we run it we will have stored all the values from the $_POST super global in variables. What are the variables named? The names of the created variables correspond to the names of the keys in $_POST, which correspond to the html name attribute of the form elements.

So in the above foreach loop, the variable $key really refers to the name of the current item in the $_POST associative array. If $key = ‘firstname’, then $$key really refers to $firstname! If we say $$key = “something”, then we have just assigned $firstname a value of “something”. In our case, the value stored will  be whatever was in the form field at the time the form was submitted. By using PHP variable variables we can automatically create variables whose names correspond to the POST data, which as you know corresponds to the name attribute on each form element.

If this concept still makes no sense, here is another attempt to explain. Normally in php, the dollar sign ($) followed by a word ($variable) denotes a variable. We can store values inside variables, e.g.

 $variable = "string";

Now by using the $$ syntax, $$variable, we are referring to whatever is stored inside (single dollar sign) $variable, in this case “string”. Basically, so long as $variable = “string”; assigning a value to $$variable is like implicitly declaring a new variable, whose name comes from the value stored in the first variable. So if we applied that to our example above:

$variable = "string";
// think of assigning $$variable a value as equivalent to assigning $string
// a value.
$$variable = 'I am stored inside $string. I was created when the programmer referred to me as $$variable.
I started out as just a simple string, stored inside $variable. But now I am moving onnnnnn up, running things!';            
echo $string; // outputs the long string above.

One Response

You can follow the comments for this article with the RSS 2.0 feed.

Thanks for the post. I learned something new today. I’m a programmer from the FoxPro days. Back in the old days in FoxPro/dBase this was called a macro. I’m glad to see it’s available in PHP

1 TheFidler August 26, 2010 1:02 pm

Leave a Reply

Required fields are marked with an asterisk (*), you may use these tags in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free

Most Recent Post

It’s been a long time, I shouldn’t have left you, part 1 …

But now I’m back.All official like.I do social stuff again.I see my friends.I’m moving into my own place again.And all that adult shit.

Categories

Content © professional dilettante
Proudly powered by WordPress
Theme designed by Artisan Themes

Entries (RSS)
Comments (RSS)

22 queries.
0.479 seconds.