TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-12-2007, 03:50 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile Functions with Endless Amounts of Arguments

Sometimes we need to create functions that require an indefinite and variable amount of arguments. Creating a function to cater for these arguments may seem straight-forward; I've seen several people who have created functions with five or so arguments and if they required a sixth argument at any stage during their project's development, they added the sixth argument to the function and carried on as normal.

Consider the function below that somebody has put together to construct an array from the arguments that are passed into the function:

php Code:
function create_array($szItem1, $szItem2, $szItem3)
{
    return array($szItem1, $szItem2, $szItem3);
}

This will work perfectly. However, you are not planning for future expansion. Suppose in a week's time you want to create an array with four arguments, what will you do then? Most people would simply add another argument and not look for a more long-term solution to the predicament.

Luckily, there is a fairly straight-forward solution, and that is to use a series of native PHP functions. You do not have to explicitly specify the arguments but rather add the arguments in the function call and let PHP do the rest. See what this looks like now we have adapted our code:

php Code:
function create_array()
{
    return func_get_args();
}

All this does is takes the arguments that we specified in the function call, and return them as an array. There's nothing more to it than that! As we do not know how many arguments we will be requiring, func_get_args takes all the arguments and returns them as an array. Our function call looks like this:

php Code:
create_array(5, 'Orange', 'Apple', 'Grape');

We can add as many arguments as we like and the function will return us an array containing those arguments. To take this a step further, we can specify explicit arguments and interpret them differently. Even when using func_get_args, we may still place arguments in to the function declaration, like so:

PHP Code:
function create_array($iLimit)
{
    
$aArgs = array();
    
    for(
$iIndex 1$iIndex func_num_args(); $iIndex++)
    {
        
$szItem func_get_arg($iIndex);
        
        if(
strlen($szItem) <= $iLimit)
        {
            
$aArgs[] = $szItem;
        }
    }
    
    return 
$aArgs;

What the above adaptation does is takes a mandatory argument. From that argument we are going to debar any fruit that has more than five characters. In our original function call, apple and grape will get through, whereas orange will be discarded. Poor orange! A couple of new functions in the above code are func_num_args which returns an integer value informing you how many arguments have been specified, and func_get_arg which returns a single argument based on the argument offset you pass it. Incidentally, we begin the $iIndex at 1 to bypass the $iLimit argument. Our two out of three fruits are then returned as an array when we call the function:

php Code:
$aShortNamedFruits = create_array(5, 'Orange', 'Apple', 'Grape');

And all works well! Using these series of functions definitely spares you from having to return to your function add more arguments if and when you need them. By using func_get_args you can specify limitless arguments and the function will carry on working regardless. If you're already wise enough and realise how to use func_get_args then you'll no doubt be happy to never return to that function again to add extra arguments. It's up to you to spread the word - life just got a little bit easier!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
Matt83 (12-11-2007), Village Idiot (12-11-2007)
Old 11-12-2007, 04:43 PM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

This could come in handy some day :).
Nor is offline  
Reply With Quote
Old 11-14-2007, 02:09 PM   #3 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Nice tip you got there,
This line :
PHP Code:
for($iIndex 1$iIndex func_num_args(); $iIndex++) 
Is not that good because on every loop it calls the "func_num_args" function, So you may replace it with :
PHP Code:
for($iIndex 1$num_args func_num_args(); $iIndex $num_args$iIndex++) 
... 
or :
PHP Code:
$num_args func_num_args();
for(
$iIndex 1$iIndex $num_args$iIndex++) 
... 
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-15-2007, 01:52 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You're quite right, Morishani! Well pointed out. There is no excuse for calling a function more than once unless its result changes every single time you require it.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-15-2007, 02:34 PM   #5 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Nice example, func_num_args() isnt very widely known/used. A VERY competant php programmer, a friend of mine, was trying to do something similar a while back, and was doing all sorts of parsing to get the variables... He was amazed when he found out about this.

Really odd how unknown this is :)
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-19-2007, 07:03 AM   #6 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Also I didn't know of this function. It will - indeed - come in handy some day!

Thanks for sharing.
maZtah is offline  
Reply With Quote
Old 12-11-2007, 03:26 PM   #7 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by maZtah View Post
Also I didn't know of this function. It will - indeed - come in handy some day!

Thanks for sharing.
Same here. Never heard of the function and this will come on handy some day. Since I'm currently developming my own CMS and all.

Thanks!
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-11-2007, 03:28 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I see a lot of people getting around this by passing in arrays to their functions - which is all fine and dandy in its right place, but it's not a workaround solution, in my opinion. If the arguments differ then in my opinion they should be as different arguments, not just different items in an array.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-11-2007, 04:10 PM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I've been wondering how to do this for ages, thanks.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:47 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design