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.

<form action="feedback.php" method="post" enctype="multipart/form-data" name="feedbackForm" id="feedbackForm">
	<label for="name">Name</label>
	<input type="text" name="name" id="name" />
	<br />
	<label for="address">Address</label>
	<input type="text" name="address" id="address" />
	<br />
	<label for="phone">Phone</label>
	<input type="text" name="phone" id="phone" />
	<br />
	<label for="fax">Fax</label>
	<input type="text" name="fax" id="fax" />
	<br />
	<label for="email">Email</label>
	<input type="text" name="email" id="email" />
	<br />
	<label for="website">Website</label>
	<input type="text" name="website" id="website" />
	<br />
	<label for="message">Message</label>
	<textarea name="message" id="message" cols="45" rows="5"></textarea>
	<br />
	<input type="submit" name="submit" id="submit" value="Submit" />
</form>

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

<?php
	saveFeedback($_POST[‘name’], $_POST[‘address’], $_POST[‘phone’], $_POST[‘fax’], $_POST[‘email’], $_POST[‘website’], $_POST[‘message’]);
?>

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:

<?php
	saveFeedback($_POST);
?>

Or the Object Oriented Programming style:

<?php
	$myObject = new MyClassName();
	$myObject->saveFeedback($_POST);
?>

Easy and comfortable. Huh?

Getting the values inside the function definition:

<?php
	function saveFeedback($inputArray)
	{
		$myName    = $inputArray[‘name’];
		$myAddress = $inputArray[‘address’];
		$myPhone   = $inputArray[‘phone’];
		$myFax     = $inputArray[‘fax’];
		$myEmail   = $inputArray[‘email’];
		$myWebsite = $inputArray[‘website’];
		$myMessage = $inputArray[‘message’];
		// Rest of the coding for this function
	}
?>

Simple. Right?

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

<?php
	function saveFeedback($inputArray)
	{
		var_dump($inputArray);
	}
?>

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:

<?php
	filterKeys($_GET);
?>

Or the Object Oriented Programming style:

<?php
	$myObject = new MyClassName();
	$myObject->filterKeys($_GET);
?>

And here is how you get them:

<?php
	function filterKeys($inputArray)
	{
		$productType      = $inputArray[‘type’];
		$productMaxPrice  = $inputArray[‘max_price’];
		$productMinPrice  = $inputArray[‘min_price’];
		$productMaterial  = $inputArray[‘material’];
	}
?>

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:

<?php
	function filterKeys($inputArray)
	{
		var_dump($inputArray);
	}
?>

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:

<?php
	$_FILES[‘fileName’][‘name’]
	$_FILES[‘fileName’][‘type’]
	$_FILES[‘fileName’][’size’]
	$_FILES[‘fileName’][‘tmp_name’]
	$_FILES[‘fileName’][‘error’]
?>

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:

<?php
	function saveClientInformation($age=25, gender=‘Male’)
	{
	}
?>

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

<?php
	function saveClientInformation($postInputArray, $getInputArray, $age=25, gender=‘Male’)
	{
	}
?>

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

<?php
	function saveClientInformation($_POST, $_GET, 35, ‘Male’);
?>

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

Thanks for reading.