Nov 9, 2008 - PHP    4 Comments

PHP: pass all POST and GET variables in array in function parameter


My subject of this post is about the total number of parameters we send as input from a form or any other way and optimizing the parameter passing. If we have many forms and many input fields in each form, it is often painful to set or order value of GET, POST or other variables one by one.

Passing all POST variables as function parameter in one array

Let us see the following HTML form that has ten fields.

Now if we want to call a function passing all the input fields, it will look like this:

But does it look good or anything comfortable? I know most coder will be bored doing this. When I was a newcomer to PHP programming, I used to do this kind of programming. But there is no reason to do coding like this as we have extremely simple solution.

Fortunately we can pass a whole array as function parameter value. This is how:

Function calling:

Or the Object Oriented Programming style:

Easy and comfortable. Huh?

Getting the values inside the function definition:

Simple. Right?

You can also print all the input values inside your function you want for debugging or any other purpose as like this:

Passing all GET variables as function parameter in one array

Suppose that here is your link:

You can pass all the GET variables to a function such as ‘filterKeys’ in this case simply this way:

Or the Object Oriented Programming style:

And here is how you get them:

This is just like the way used for the POST method.

You can print the input values inside your function anytime you want for debugging or any other purpose like this:

Although REQUEST method will work similarly, I suggest you not using this for avoiding any future confusion and also for the sake of keeping your coding more readable for other developers.

Special Case: File

Actually when you send a file, it contains several data and $_FILES is an array itself. So, the following values come directly to your script without any extra attention:

So, you can use them directly in your script.

Taking another Special Case in Consideration: Setting default input values

Sometimes you may need to set a default value to one or more parameters. For example:

You can define array and scalar inputs altogether as like this:

Next you can pass array and scalar inputs altogether as like this:

So, why should you code like this? Because this will increase efficiency, save some time and make your code clean looking.

Thanks for reading.


  • I want to know about PHP map function??
    [email protected]

    plz mail me in this Url

  • Hi.
    Do you have tips on how to pass POST variables as array to a separate .php file, and executing a confirmation message, either in the separate .php file or in the form file? I can’t get it to work.

  • hi ,

    i have used your example :

    function filterKeys($inputArray)

    to get everything thats posted to a page, my question is :

    on the posting page i have some code that allows you to added more fields to enter text in to to send to the page with the above code on, the code on the posted page adds 1, 2, 3, 4, after the form item name IE

    how can i capture this dynamically posted details?

    many thanks

  • I have read this ‘old’ article and have asked a question about it at Stack Overflow. I thought it would be nice to tell you about it:

Got anything to say? Go ahead and leave a comment!