note 39075 added to language.types.array

From: Date: Fri, 16 Jan 2004 05:19:14 +0000
Subject: note 39075 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-63666@lists.php.net to get a copy of this message
IMPLEMENTING FUNCTIONS WITH A VARIABLE NUMBER OF PARAMETERS If you're like me, you eventually start building complex functions that require a lot of parameters (often 6 or more). The good folks at PHP recommend you do the following, which probably works for most people: EXAMPLE 1: THE TRADITIONAL WAY <?php function myFunctionName($first_name, $last_name, $action = "was not", $result = "pushed") { // function code here echo sprintf("%s %s %s %s", $first_name, $last_name, $action, $result); } // This returns "Humpty Dumpty was pushed" myFunctionName("Humpty", "Dumpty", "was"); ?> It works, but you are forced to: (1) ALWAYS provide the first two parameters ($first_name and $last name - $action and $result are optional), and (2) ALWAYS follow the exact order that the functions expects them to be in. However, I often find myself needing more flexibility than Example 1 offers... sometimes I only want to specify $action, but don't want to type ALL of the preceding parameters when I call the function. 'So what's a poor coder to do?', you ask. Well, you can keep rearranging the parameters throughout your code until you're happy, OR you can pass all of your parameters as an array, as follows: EXAMPLE 2: PASSING PARAMETERS IN AN ARRAY <?php function myFunctionName($arguments) { extract($arguments); // turns array elements into variables if (empty($action)) { $result = "was not"; } if (empty($result)) { $result = "pushed"; } echo sprintf("%s %s %s %s", $first_name, $last_name, $action, $result); } // This also returns "Humpty Dumpty was pushed" myFunctionName(array("action" => "was", "last_name" => "Dumpty", "first_name" => "Humpty")); ?> Example 2 is more flexible than it may seem at first. Your first thought may have been, 'hey, that's stupid, it's more complex than it needs to be!' But if you look at it for a moment, you'll notice that it allows you to: (1) specify ONLY the parameters you want to, and (2) the order you pass them into the function DOES NOT matter (3) You can still define the default values for each argument within the function - I used if() statments, but you could also use switch() if you have more advanced needs. I could further refine Example 2 so that I only need to supply ONE parameter like I wanted; to do that, I would add defaults for $first_name and $last name in the function in Example 2, which means I could simplify my function call to just this: <?php // This also returns "Humpty Dumpty was pushed" myFunctionName(array("action" => "was")); ?> I dunno about you, but if I've got a complex function that can take a lot of parameters, I'd rather supply one arguments instead of several. It's a different approach, but in specific circumstance it not only works and is flexible, but will save you a lot of hassle. ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39075 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39075&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39075&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63666) next »