如何在 PHP 中在第一个可选参数之后设置可选参数

发布于 2024-07-29 03:36:50 字数 827 浏览 3 评论 0原文

我对 php 相当陌生,我想弄清楚如何在第一个可选参数之后设置可选参数?

例如,我有以下代码:

function testParam($fruit, $veg='pota',$test='default test'){
echo '<br>$fruit = '.$fruit;
echo '<br>$veg = '.$veg;
echo '<br>Test = '.$test;
}

如果我进行以下调用:

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');

我想尝试进行调用,以便在最后一个示例中我将“pota”显示为默认的 $veg 参数,但传递到 $test “i am set”。

我想我可以将 0 传递到 $veg 中,然后在代码中分支它,如果 $veg =0 则使用 'pota' 但只是想知道是否还有其他语法,因为我在 php.net 中找不到任何关于它的内容。

I'm fairly new to php and I am trying to figure how do I set an optional parameter after the first optional parameter?

For example I have the following code:

function testParam($fruit, $veg='pota',$test='default test'){
echo '<br>$fruit = '.$fruit;
echo '<br>$veg = '.$veg;
echo '<br>Test = '.$test;
}

If i make the following calls:

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');

I want to try make a call so that in the last example I show 'pota' as the default $veg parameter but pass into $test 'i am set'.

I guess I can pass 0 into $veg then branch it in the code to say if $veg =0 then use 'pota' but just wondered if there's some other syntax as i cant find anything in php.net about it.

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

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

发布评论

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

评论(3

花辞树 2024-08-05 03:36:50

仅使用默认参数无法完成您想要的操作。 默认值仅适用于缺少的参数,并且只能缺少最后一个参数。

您可以添加诸如

  $vega = $vega ? $vega : 'carrot';

和 call the function as 之

testParam('apple',false,'something');

类的行,也可以使用更通用的技术,以参数名称作为键在数组中传递参数。 像这样的东西

function testparam($parms=false) {
    $default_parms = array('fruit'=>'orange', 'vega'=>'peas', 'starch'=>'bread');
    $parms = array_merge($default_parms, (array) $parms);
    echo '<br>fruit  = $parms[fruit]';
    echo '<br>vega   = $parms[vega]';
    echo '<br>starch = $parms[starch]';
}

testparm('starch'=>'pancakes');
//we get:
//fruit = orange
//vega  = peas
//starch = pancakes

有点冗长,但也更灵活。 您可以添加参数和默认值,而无需更改现有的调用者。

You can't do what you want with just default parameters. The defaults only apply to missing arguments, and only the last argument(s) can be missing.

You can either add lines like

  $vega = $vega ? $vega : 'carrot';

and call the function as

testParam('apple',false,'something');

or use the more general technique of passing the parameters in an array with the parameter names as keys. Something like

function testparam($parms=false) {
    $default_parms = array('fruit'=>'orange', 'vega'=>'peas', 'starch'=>'bread');
    $parms = array_merge($default_parms, (array) $parms);
    echo '<br>fruit  = $parms[fruit]';
    echo '<br>vega   = $parms[vega]';
    echo '<br>starch = $parms[starch]';
}

testparm('starch'=>'pancakes');
//we get:
//fruit = orange
//vega  = peas
//starch = pancakes

This is a little more verbose but it is also more flexible. You can add parameters and defaults without changing the existing callers.

—━☆沉默づ 2024-08-05 03:36:50

不幸的是,您无法在 PHP 中做到这一点。

您必须传入 0 或 null 或其他值,然后如果该值为 0 或 null,请将其更改为默认值。

这里< /a> 是另一个应该为您提供更多信息的问题。

Unfortunately, you cannot do that in PHP.

You have to pass in 0, or null, or some other value and then if the value is 0 or null, change it to the default value.

Here is another question that should give you more information.

深海不蓝 2024-08-05 03:36:50

这是我使用的技术:

function testParam($fruit, $veg='pota', $test='default test') {

    /* Check for nulls */
    if (is_null($veg))  { $veg = 'pota'; }
    if (is_null($test)) { $test = 'default test'; }

    /* The rest of your code goes here */
}

现在要使用任何可选参数的默认值,只需像这样传递 NULL 即可。

testParam('apple', null, 'some string');

在此示例中,$veg 将等于 'pota'

此代码示例的缺点是您必须对默认值进行两次编码。 您可以轻松地在参数声明中将默认值设置为 null,这样您就不必对默认值进行两次编码,但是,我喜欢设置两次,因为我的 IDE 为我提供了参数提示,可以立即向我显示默认值在函数签名中。

This is the technique I use:

function testParam($fruit, $veg='pota', $test='default test') {

    /* Check for nulls */
    if (is_null($veg))  { $veg = 'pota'; }
    if (is_null($test)) { $test = 'default test'; }

    /* The rest of your code goes here */
}

Now to use the default value of any optional parameter, just pass NULL like so.

testParam('apple', null, 'some string');

In this example, $veg will equal 'pota'

The downside of this code example is that you have to code the default values twice. You could just as easily set the default value to null in the parameter declaration so you don't have to code the default value twice, but, I like to set it twice because my IDE gives me parameter hinting that instantly shows me the default values in the function signature.

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