Freedom, Community & Sustainability

PHP: grab all post or get variables

January 2, 2011 -- William
Last modified on September 2014
Duration: 5 minutes or less

-Set all posts or get variables in one loop-

Simply adapt the following loop to your needs. The foreach loop will copy all POST or GET variables you are receiving from another page into variables that you can use on the present page. Here it goes:

foreach ($_POST as $var => $value) {
    $$var = $value;

The variables will all be set according to the name of the inputs, with the values given into post.

for example:
on the form you have:

<input type=text name=variable1 value=value1>

When you use the loop above you will have $variable1 set to ‘value1’.

If you are using the get method on your form just change the input of the loop.

foreach ($_GET as $var => $value) {
    $$var = $value;

You can also make just whatever treatment you want inside the loop like for example show the content of all variables:

foreach ($_GET as $value) {
    echo $value;

Add new comment