Reply to Re: Functions with variable arguements?

Your name:

Reply:


Posted by Koncept on 11/15/06 21:24

In article <Abj5h.2700$ae6.1636@clgrps13>, sTony
<rWorldDesigns@yahoo.ca> wrote:

> I've now got several versions of what is virtually the same function. I've
> got a createLink($Link); and a createSpecialLink($Link,$Name) and a
> createExtraLink($Link,$Extra) and now I've come across a situation where I
> need to write yet another almost exactly the same function. I know that php
> must support functions with a variable number of parameters, but I have no
> idea how to write one. How is this done in php, so I can have just one
> function that does different things according to the information supplied.
>
> Thanks in advance,
>
> sTony
>
>

Take a look at func_num_args() and func_get_args()

<?php
function multiArgs() {
list( $numArgs, $args )
= array( func_num_args(), func_get_args() );
switch( $numArgs ){
case 1:
// one var passed
break;
case 2:
// two vars passed
echo "You passed two vars: "
. join( ', ', $args ) . "<br />\n";
break;
case 3:
// three vars passed
break;
default:
// error or default handler for
echo "You passed $numArgs vars: "
. join( ', ', $args ) . "<br />\n";
break;
}
// do stuff...
}

multiArgs( 'foo', 'bar' );
multiArgs( 'is', 'this', 'what', 'you', 'want', 'to', 'do?' );
?>

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация