You are here: Re: Functions with variable arguements? « All PHP « IT news, forums, messages
Re: Functions with variable arguements?

Posted by dimo414 on 11/14/06 00:30

sTony 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

PHP does not in fact allow function overloading, ie same name,
different signature. But it does allow you to use optional variables,
simply give them a default value in the parameters list.

For instance, you might make an exponent method, which takes a base and
a power, like this:
exponent($base, $power=2)
{
// operation
}

Then you can call it either by the normal way: exponent(3, 3) which
would return 27, or you could call exponent(4), and because the default
power is 2, it'll be squared, and return 16.

Hope that makes sense.

 

Navigation:

[Reply to this 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

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