View Full Version : building a function with an optional argument
sabret00the
07-04-2005, 07:03 PM
how would i go about doing the above
function getpiece($id = FALSE)
global $p;
if (!$p)
{
$p = intval($id); // if $p exists $id won't exist
}
how would i go about doing this?
Colin F
07-04-2005, 07:13 PM
how would i go about doing the above
function getpiece($id = FALSE)
global $p;
if (!$p)
{
$p = intval($id); // if $p exists $id won't exist
}
how would i go about doing this?
if, in the functions arguments, you define something with a =, it'll use that value if nothing is entered.
so you can do this:
function foo($firstvar)
{
}this
function foo($firstvar = 'standardvalue')
{
}this
function foo($firstvar = 'standardvalue', $secondvar = 'standardaswell')
{
}this,
function foo($firstvar, $secondvar = 'standard')
{
}and so on.
Any arguments that are needed have to be at the beginning though.
Andreas
07-04-2005, 07:14 PM
I don't understand your question?
sabret00the
07-04-2005, 08:41 PM
basically i have the function getpiece
function getpiece($id = 'nothere')
{
global $vboptions, $stylevar, $vbphrase, $bgclass, $DB_site, $p;
if ($id != "nothere")
{
$p = $id;
}
$getthepiece = $DB_site->query_first("
## query ##
");
return $getthepiece;
most of the times i call getpiece() via
$array = getpiece(); // for this is uses the $p
but i need to call it in another function with $array = getpiece($pieceid); // because $p doesn't exist
Andreas
07-04-2005, 08:51 PM
I still don't get what you want, but maybe it's this:
function getpiece()
{
global $vboptions, $stylevar, $vbphrase, $bgclass, $DB_site, $p;
if (func_num_args() > 0)
{
// Called with Parameters, so we take the first parameter as $id
$id = func_get_arg(0);
}
else
{
// Called with no Parameters, so we take $p as $id
$id = $p;
}
$getthepiece = $DB_site->query_first("
Paul M
07-04-2005, 08:53 PM
Difficult to answer without seeing the query - does it pull some data based on the value of $p ?
Why don't you just make the first one ;
$array = getpiece($p);
and then just make the function return the appropiate data from the query using $id.
sabret00the
07-11-2005, 08:58 AM
thanks all for your help, in the end i got it to work using function($var) for both calls
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.