^ A time Saver Script

Submitted on: 1/1/2015 12:50:00 PM
By: Darryl Porter (from psc cd)  
Level: Beginner
User Rating: By 24 Users
Compatibility: PHP 4.0
Views: 251
     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.

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 1 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

 There are no comments on this submission.

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.