传递 NULL 参数与不传递参数完全相同

发布于 2024-10-10 08:54:52 字数 371 浏览 0 评论 0原文

我正在使用一个函数,其签名如下所示

afunc(string $p1, mixed $p2, array $p3 [, int $p4 = SOM_CONST [, string $p5 ]] )

在某些情况下,我没有要传递的最后一个参数 $p5 的数据,但为了保持一致性,我仍然想传递类似的内容NULL。所以我的问题是,PHP 将传递 NULL 与不传递任何内容完全一样吗?

somefunc($p1, $p2, $p3, $p4 = SOM_CONST);
somefunc($p1, $p2, $p3, $p4 = SOM_CONST, NULL);

I'm working with a function whose signature looks like this

afunc(string $p1, mixed $p2, array $p3 [, int $p4 = SOM_CONST [, string $p5 ]] )

In some cases I don't have data for the last parameter $p5 to pass, but for the sake of consistency I still want to pass something like NULL. So my question, does PHP treat passing a NULL exactly the same as not passing anything?

somefunc($p1, $p2, $p3, $p4 = SOM_CONST);
somefunc($p1, $p2, $p3, $p4 = SOM_CONST, NULL);

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

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

发布评论

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

评论(4

半窗疏影 2024-10-17 08:54:52

不。据推测,确切的签名是:

function afunc($p1, $p2, $p3, $p4 = SOM_CONST)

并且该函数使用 func_get_argfunc_get_args 表示 $p5

func_num_args 将有所不同,具体取决于您是否传递 NULL。如果我们这样做:

{
    echo func_num_args();
}

然后

afunc(1,2,3,4,NULL);

给出

afunc(1,2,3,4);

5 和 4。

函数是否以相同的方式处理它们取决于函数。

No. Presumably, the exact signature is:

function afunc($p1, $p2, $p3, $p4 = SOM_CONST)

and the function uses func_get_arg or func_get_args for $p5.

func_num_args will be different depending whether you pass NULL. If we do:

{
    echo func_num_args();
}

then

afunc(1,2,3,4,NULL);

and

afunc(1,2,3,4);

give 5 and 4.

It's up to the function whether to handle these the same.

小情绪 2024-10-17 08:54:52

否。仅当参数的默认值为 NULL 时才相同,否则不会:

function a($a, $b, $c = 42) {
    var_dump($c);
}

a(1, 2, NULL);

打印 NULL

所以你不能说它总是一样的。 NULL 只是另一个值,并没有什么特殊之处。


更不用说非可选参数了:调用 a(1) 会给您一个错误(警告),但 a(1,NULL) 可以工作。

No. It is only the same if the parameter's default value is NULL, otherwise not:

function a($a, $b, $c = 42) {
    var_dump($c);
}

a(1, 2, NULL);

prints NULL.

So you cannot say it is always the same. NULL is just another value and it is not that special.


Not to mention non-optional parameters: Calling a(1) would give you an error (warning) but a(1,NULL) works.

以可爱出名 2024-10-17 08:54:52

传递 NULL 与不传递参数完全相同 - 如果默认值为 NULL,除非在 func_num_args 函数:

返回传递的参数数量
到函数。

<?php

function test($test=NULL) {
    echo func_num_args();
}

test(); //outputs: 0
test(NULL); //outputs: 1

?>

Passing NULL is exactly the same as not passing an argument - if the default value is NULL, except in the context of the func_num_args function:

Returns the number of arguments passed
to the function.

<?php

function test($test=NULL) {
    echo func_num_args();
}

test(); //outputs: 0
test(NULL); //outputs: 1

?>
掀纱窥君容 2024-10-17 08:54:52

首先,你的函数签名不正确。强烈建议在 PHP 中将可选参数保留在所有非可选参数之后。

此时,如果您需要跳过某些可选参数,可以提供 NULL 作为它们的值,函数将使用它们的默认值。它类似于在 $x = $x ?? 中使用空合并运算符。 12..仅当 $x === NULL 时,此代码才会将 $x 设置为 12

这里甚至不应该提及使用 func_num_args() 的参数数量,因为无论参数的值是什么,如果您在函数调用中传递参数,那么该参数将被注意到的功能。当涉及到函数的内部工作时,额外的 NULL 参数与任何其他值一样好。您的代码可能需要一个 NULL 参数才能执行某些任务。

当可选函数参数填充有 NULL 参数时,才会发生唯一的特殊处理。

First of all, your function signature is incorrect. It is highly recommended in PHP to keep the optional parameters after all the non optional parameters.

At that point, if you need to skip some of the optional parameters, you can provide NULL as their values and the function will use their default value. It will be similar to using the null coalesce operator in $x = $x ?? 12. This code will set $x to 12 only if $x === NULL.

Using the number of arguments with func_num_args() should not even be mentioned here because no matter what the value of the argument is, if you pass an argument in the function call, then that argument will be noticed by the function. And when it comes to the inner workings of the function, an extra NULL argument is just as good as any other value. Your code might need an argument to be NULL in order to perform some task.

The only special treatment happens when an optional function parameter is filled with a NULL argument.

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