基于参数返回函数不同部分的PHP

基于参数返回函数不同部分的PHP,php,function,parameters,Php,Function,Parameters,有没有一种方法可以根据函数中传递的内容只返回函数的一部分?例如: function test($wo) { if function contains $wo and "date" { //pass $wo through sql query to pull date return $date } if function contains $wo and "otherDate" { //pass $wo through another sql query to pull another dat

有没有一种方法可以根据函数中传递的内容只返回函数的一部分?例如:

function test($wo) {

if function contains $wo and "date" {
//pass $wo through sql query to pull date
return $date
}

if function contains $wo and "otherDate" {
//pass $wo through another sql query to pull another date
return $otherDate

}

if function only contains $wo {
//pass these dates through different methods to get a final output 
return $finaldate

}

}
日期:

返回:

1/1/2015
10/01/2015
其他日期:

test($wo, otherDate);
返回:

1/1/2015
10/01/2015
正常输出:

test($wo);
返回:

12/01/2015

你的问题很模糊,但如果我理解正确的话,你需要可选的参数。通过在函数定义中为函数参数提供默认值,可以使函数参数成为可选参数:

// $a is required
// $b is optional and defaults to 'test' if not specified
// $c is optional and defaults to null if not specified
function test($a, $b = 'test', $c = null)
{
    echo "a is $a\n";
    echo "b is $b\n";
    echo "c is $c\n";
}
现在您可以执行以下操作:

test(1, 'foo', 'bar');
你会得到:

a is 1
b is foo
c is bar
a is 37
b is test
c is
或者这个:

test(37);
你会得到:

a is 1
b is foo
c is bar
a is 37
b is test
c is

传递指定返回内容的参数:

function test($wo, $type='final') {
    // pull $date
    if($type == 'date') { return $date; }
    // pull $otherdate
    if($type == 'other') { return $otherdate; }
    // construct $finaldate
    if($type == 'final') { return $finaldate; }

    return false;
}
然后像这样打电话:

$something = test($a_var, 'other');
// or for final since it is default
$something = test($a_var);  

您必须给出一个更好的示例,说明预期的输入/输出等。希望现在更清楚,switch/case可能也是一个很好的建议。@SuperJer:是的,我是这样开始的,但是如果$final需要$date和$other,我不希望出现下一个案例。谢谢,这正是我需要的