Quick Search for:  in language:    
   Code/Articles ╗ |  Newest/Best ╗ |  Community ╗ |  Jobs ╗ |  Other ╗ |  Goto ╗ | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
PHP Stats

 Code: 66,707 lines
 Jobs: 14 postings

Sponsored by:


You are in:


Latest Code Ticker for PHP.
Simple Forum
By Chas Pardee on 6/20

Click here to see a screenshot of this code!Squiggles
By Chas Pardee on 6/20

(Screen Shot)

photo guide
By Chas Pardee on 6/20

Download Script
By YANP on 6/19

Logging IP's
By Klaus Andersen on 6/18

Server Uptime
By Martin C. Conniffe on 6/15

Send a Page/Link to a Friend
By rae the coder on 6/15

Display contents of a directory and include it in a text area
By Daniel Friedman on 6/15

By Alberto Sartori on 6/13

Click here to put this ticker on your site!

Add this ticker to your desktop!

Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites




Submitted on: 12/26/2001 4:18:14 PM
By: Charles Chadwick 
Level: Beginner
User Rating: By 3 Users
Compatibility:PHP 3.0, PHP 4.0

Users have accessed this article 8276 times.

(About the author)
     This describes how to use while(), list() and each() in conjunction with the predefined PHP variables $HTTP_POST_VARS and $HTTP_GET_VARS to read information passed to a page without knowing the variable names, or how many variables were passed.

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 langauges 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.
Looping Through $HTTP_GET_VARS and $HTTP_POST_VARS

Quick Associative Array Overview

First off, to understand how to use these variables, you must understand what an associative array is. If you already know, then you can skip this part.

An associative array is simply an array who's key is not a number (i.e. $myArray[0], $myArray[1], etc.) but a word, as it were. For instance, if I have an associative array named "$myAssoc" who's values are the persons last name, and the keys are their first names, then it would look like this.

$myAssoc["charles"] = "chadwick";
where "charles" is the key (like 0, 1, etc) and "chadwick" is the value.

If you want a better explanation of this, I suggest reading the associative array information in the PHP manual found at

Reading Variables Passed Through GET and POST

For this example, I am going to use a standard while() loop to help us traverse through the associative array and read both it's keys and values. This will, as I am sure you are aware, execute until such time as there are no more keys to be read. So our first bit of code will use not only the while() statement, but two others, list() and each(). List() is used to assign a list of variables specified values in one operation. The each() function will pull a key and a value out of an array, and then advance the pointer to the next element in the array. So the first part of the code will look like so.

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

This code is basically using the list function to assign information to our $key and $value variables, which is coming from the each() function. Then all we need to do is echo this information out to our page.

while(list($key, $value) = each($HTTP_GET_VARS))
echo "$key = $value(br)";

NOTE: At the end of my echo statement, I have put parens () around my HTML line break because it won't display on this page otherwise. If you are using this code on a site, make sure to replace the parens with actual HTML opening and closing brackets. Also note that both the equal sign and the HTML line break are NOT necassary in this code.

Let's say that our url is this:;=two&var3;=three&var4;=four

This is the GET method. Our variables are "var1", "var2", "var3", and "var4". Their respective values in this example are "one", "two", "three", and "four". If we pass this along to a page that has our code on it, then the output would be like so:

var1 = one
var2 = two
var3 = three
var4 = four

If we are using the POST method, with the same variables, we would have the same output. However, you would need to use the $HTTP_POST_VARS variable.

NOTE: If you are using these, or any other predefined PHP variables in a function, you must declare them as global inside the function or this won't work.

That's it. This is pretty much beginner information, but I went for a while without realizing that this could be done, and it caused me to spend a lot more time coding than need be.

Other 2 submission(s) by this author


Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
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
2/6/2002 9:55:07 PM:Dustin
Great little tutorial. It was exactly what I was looking for. Thanks!
Keep the Planet clean! If this comment was disrespectful, please report it:

Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.


Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | PHP Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyrightę 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.