PHP 是否有“命名参数”?这样可以省略前面的论点而可以写后面的论点吗?

发布于 2024-08-09 09:55:16 字数 425 浏览 3 评论 0原文

在 PHP 中,只要参数具有如下所示的默认值,您就可以调用不传入参数的函数:

function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
    echo "$t1, $t2, $t3";
}
test();

但是,我们只是希望最后一个参数不同,但前两个参数应使用其默认值。我能想到的唯一方法是这样做但没有成功:

test('test1','test2','hi i am different');

我尝试过:

test(,,'hi i am different');
test(default,default,'hi i am different');

是否有干净、有效的方法来做到这一点?

In PHP you can call a function with no arguments passed in so long as the arguments have default values like this:

function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
    echo "$t1, $t2, $t3";
}
test();

However, let's just say I want the last one to be different but the first two parameters should use their default values. The only way I can think of is by doing this with no success:

test('test1','test2','hi i am different');

I tried this:

test(,,'hi i am different');
test(default,default,'hi i am different');

Is there clean, valid way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

命比纸薄 2024-08-16 09:55:16

使用数组:

function test($options = array()) {
    $defaults = array(
        't1' => 'test1',
        't2' => 'test2',
        't3' => 'test3',
    );
    $options = array_merge($defauts, $options);
    extract($options);
    echo "$t1, $t2, $t3";
}

这样调用你的函数:

test(array('t3' => 'hi, i am different'));

Use arrays :

function test($options = array()) {
    $defaults = array(
        't1' => 'test1',
        't2' => 'test2',
        't3' => 'test3',
    );
    $options = array_merge($defauts, $options);
    extract($options);
    echo "$t1, $t2, $t3";
}

Call your function this way :

test(array('t3' => 'hi, i am different'));
_蜘蛛 2024-08-16 09:55:16

使用原始 PHP 无法做到这一点。您可以尝试以下操作:

function test($var1 = null, $var2 = null){
    if($var1 == null) $var1 = 'default1';
    if($var2 == null) $var2 = 'default2';
}

然后调用您的函数,并使用 null 作为默认变量的标识符。您还可以使用具有默认值的数组,使用更大的参数列表会更容易。

更好的是尽量避免这一切,并重新考虑一下你的设计。

You can't do that using raw PHP. You can try something like:

function test($var1 = null, $var2 = null){
    if($var1 == null) $var1 = 'default1';
    if($var2 == null) $var2 = 'default2';
}

and then call your function, with null as the identifier of the default variable. You can also use an array with the default values, that will be easier with a bigger parameter list.

Even better is to try to avoid this all, and rethink your design a bit.

﹂绝世的画 2024-08-16 09:55:16

在 PHP 中,具有默认值的参数必须位于最后,在其他参数之后,并且在调用函数时必须填写到此为止的所有其他参数。我不知道如何传递触发默认值的值。

The parameters with default values have to be last, after the others, in PHP and all the others up to that point must be filled in when calling the function. I don't know of anyway to pass a value that triggers the default value.

吹泡泡o 2024-08-16 09:55:16

在这些情况下,我通常所做的是将参数指定为数组。看看下面的例子(未经测试):

<?php
test(array('t3' => 'something'));

function test($options = array())
{
  $default_options = array('t1' => 'test1', 't2' => 'test2', 't3' => 'test3');
  $options = array_merge($default_options, $options);

  echo $options['t1'] . ', ' . $options['t2'] . ', ' . $options['t3'];
}
?>

What I normally do in those situations is to specify the parameters as an array instead. Have a look at the following example (untested):

<?php
test(array('t3' => 'something'));

function test($options = array())
{
  $default_options = array('t1' => 'test1', 't2' => 'test2', 't3' => 'test3');
  $options = array_merge($default_options, $options);

  echo $options['t1'] . ', ' . $options['t2'] . ', ' . $options['t3'];
}
?>
打小就很酷 2024-08-16 09:55:16

您可以像这样定义函数:

function grafico($valores,$img_width=false,$img_height=false,$titulo="title"){
    if ($img_width===false){$img_width=450;}
    if ($img_height===false){$img_height=300;}
    ...
   }

并在没有持久参数的情况下调用它或
将一个或多个替换为“false”:

grafico($values);
grafico($values,300);
grafico($values,false,400);
grafico($values,false,400,"titleeee");

you could define the function like:

function grafico($valores,$img_width=false,$img_height=false,$titulo="title"){
    if ($img_width===false){$img_width=450;}
    if ($img_height===false){$img_height=300;}
    ...
   }

and call to it without the lasting params or
replacing one or several with "false":

grafico($values);
grafico($values,300);
grafico($values,false,400);
grafico($values,false,400,"titleeee");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文