Tagged with " variable"
Nov 9, 2008 - PHP    4 Comments

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

Hello!

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:
http://www.example.com/products.php?type=cap&max_price=1000&min_price=100&material=leather

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.

Mar 29, 2008 - PHP    13 Comments

PHP script to print all the GET & POST variables

Variables are one of the core powers of programming. As PHP programmers, we oftern may find that a $_GET or $_POST variable is not performing properly or the way we expected. This may occur for several reasons such as we made a mistake in the variable name. Sometimes this kind of problem take so much time that lots of time is wasted to correct a single variable!

So, you can use the scripts I have written below and use in your code whenever you suspect that there may be a problem with the variables. These scripts helps you by showing the details of each and every $_GET and $_POST variables in the script.

This is very easy and actually 3 (three) lines of code required.

Below is the code to print all $_GET variables:

Click here to see the demo here.

To print all the $_POST variables, we need to change only one line:

OK.
You can do it in another way too. It is simple also.

Or,

So, print your $_GET and $_POST variables whenever you think required.