在PHP 4/5中是否可以在调用时指定一个命名的可选参数,跳过你不想指定的参数(比如在python中)?
就像是:
function foo($a,$b='', $c='') {
// whatever
}
foo("hello", $c="bar"); // we want $b as the default, but specify $c
Run Code Online (Sandbox Code Playgroud)
谢谢
我对php很新,我试图在第一个可选参数后设置一个可选参数?
例如,我有以下代码:
function testParam($fruit, $veg='pota',$test='default test'){
echo '<br>$fruit = '.$fruit;
echo '<br>$veg = '.$veg;
echo '<br>Test = '.$test;
}
Run Code Online (Sandbox Code Playgroud)
如果我拨打以下电话:
echo 'with all parama';
testParam('apple','carrot','some string');
//we get:
//with all parama
//$fruit = apple
//$veg = carrot
//Test = some string
echo '<hr> missing veg';
testParam('apple','','something');
//we get:
//missing veg
//$fruit = apple
//$veg =
//Test = something
echo '<hr> This wont work';
testParam('apple',,'i am set');
Run Code Online (Sandbox Code Playgroud)
我想尝试拨打电话,以便在最后一个例子中我将'pota'显示为默认的$ veg参数,但是传入$ test'我设置'.
我想我可以将0传递给$ veg然后在代码中分支,如果$ veg = 0然后使用'pota',但只是想知道是否有其他语法,因为我无法在php.net中找到任何关于它的内容.
我正在尝试构建一个具有2个默认值的函数,但在某些情况下我只需要提供第一个参数,而其他只需要第二个参数.在PHP中有没有办法可以做到这一点,或者有没有办法做我想要的标准?
例
function ex($a, $b = null, $c = null) {
....
}
ex($a, $b, $c) // WORKS FINE
ex($a) // WORKS FINE
ex($a, $b) // WORKS FINE
ex($a, $c) // WANT VAR C TO BE VAR C IN THE FUNCTION AND NOT VAR B
Run Code Online (Sandbox Code Playgroud)
谢谢