Important alert: (current site time 12/18/2014 5:39:26 PM EDT)


^ A time Saver Script

Submitted on: 10/9/2002 3:03:22 PM
By: Darryl Porter 
Level: Beginner
User Rating: By 24 Users
Compatibility: PHP 4.0
Views: 27634
author picture
(About the author)
     In the latest version of PHP, you must use GET and POST arrays to pass variables--That's not bad--you just write $foo=$_GET["foo']; and you use $foo--But say you have 80 variable to declare--then it gets time consuming. This will cut the time to nothing.

Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
With the advent of newer version of PHP you can't do some of the convenient stuff that you used to be able to do. One of which is, you can no longer pass variable from one page to the other with a form without using the super-global variables $_POST or $_GET. These were introduced for security reasons, but it make coding that much longer because before you can use a variable you must declare it using the super-global so your code end up looking like this:

<form method=POST>
<input="text" name="foo" value="">

You pass this form to the next page and your code looks like this:
$foo = $_POST["foo"];

Now you can use $foo like in older version of PHP. You might say,
what's wrong with that. I say nothing--unless you have
over 80 variable to declare, then it becomes tedious.
However, with this little script, it does the work for you.

/*I use a WHILE LOOP to run throught the GET or POST array.
You could also use a FOREACH LOOP. If you are using $_GET, just replace $_POST with $_GET */

 while(list($key,$value)= each($_POST)) 

/* Creates a file called variable */
$file = "variables.php"; 
$handle = fopen ($file, "a+");
/*Replace $_POST with $_GET here as well if you need to.*/
fwrite ($handle, "<?php $$key = \$_POST[\"$key\"]; ?>"); 
fclose ($handle);
/* Includes the file filled with your varaibles. You can now use the variable like you used to under the previous versions of PHP. */
include ("variables.php");

/* Put this at the bottom of your page. It destroys the file "variables.php" */
if (file_exists($file)) { 
And that is that. If you like it, please vote for it, or else just leave a comment.

Other 2 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

10/27/2002 6:52:40 AM

Wouldn't it be lots easier just to code a loop that walks through your array an declares the variables new? Seems a little bit too complicated to write the variables into a seperate file and to include it later....
(If this comment was disrespectful, please report it.)

10/27/2002 6:19:12 PMnkans

If more than one use at one time then it may result in conflict with the variable values? Is it not ? Is there anyway to get away with multiple files to avoid clashes with the valus of post data with the simultaneous users of the script.

(If this comment was disrespectful, please report it.)

3/20/2003 5:10:37 AM

Clever, but yeah, for normal use it would be just as easy to create the variables on the fly.

However, where I can see a script like this being really useful is when you create a form for a database.. with say 200+ fields - then need to generate a table, create appropriate queries etc. could simply create the form, send the data to a script like this and voila: it creates all your standard queries for you, generates a table schema etc..

anyone wanna have a go at that?
(If this comment was disrespectful, please report it.)

6/4/2003 6:30:49 AMAaron Mueller

Good Idea!
(If this comment was disrespectful, please report it.)

8/5/2003 8:33:14 AMlixlpixel

if(phpversion()>="4.2.0") extract($_POST);

or if you're not sure where your variables come from

extract($_GET);< BR>extract($_COOKIE);

top of your old script (or better put it on a file included in every page of your script) and
you're all set.

(sometimes life can be sooo easy)
(If this comment was disrespectful, please report it.)

12/23/2003 2:52:27 PMAddict

It would be much better to create the variables on the fly. If your site gets alot of traffic, you WILL have issues with file sharing.
(If this comment was disrespectful, please report it.)

2/18/2004 3:08:30 AM

extract() is a much better way of doing it. Look at It says the function has been available since PHP 3.0.7

If you really don't want to use extract, use this:
foreach ($_POST as $name => $val) {
$$name = $val;
(If this comment was disrespectful, please report it.)

3/20/2004 4:10:13 AMPHP Duo

I was looking for such a script because it's useful when when variables are sent through fields includes via 'QUERY STRING'.
Also thanks for the alternative. Chmodding won't be needed then.
Swaroop :D
(If this comment was disrespectful, please report it.)

8/11/2004 1:33:30 PM

Why would you be passing 80 variables to a page? How much longer does it take to type $_POST['var'] than $var? voted 1
(If this comment was disrespectful, please report it.)

9/15/2004 7:50:48 PMMicheal Winger

Lol, Its a good post, because it got my attention, and then the comments on it showed me a better one, i really like that one comment that had the same thing put into one line, gj to whoever put that up. but yeah the huge code is pretty much useless.
(If this comment was disrespectful, please report it.)

5/4/2005 8:29:31 PM

this code open a security hole - if malicious put some code into form fields, it will be done by PHP! : fill the form field by 'echo('vuala!');'... Don't play with incoming variables values, it may be dangerous.
(If this comment was disrespectful, please report it.)

1/5/2006 1:44:58 PMStijn

Or you can use this:
foreach($_POST as $key=>$val) {$$key = $val;}. You can use this easily for databases too: foreach(mysql_fetch_row($sql) as $key=>$val) {$$key = $val;}.
(If this comment was disrespectful, please report it.)

9/21/2007 3:11:13 PMJason Vetter

(If this comment was disrespectful, please report it.)

12/9/2008 8:25:13 AM

Good job. What happens to your server when a thousand uses are connected to your site?
(If this comment was disrespectful, please report it.)


Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.