PHP-将NLL传递给函数的最佳方法是什么


PHP- What is the best way to pass NULL to a function?

我有一个函数,我正在尝试将 NULL 值传递给:

function test($this=null, $that=null) {
  if ($this) {
    // Do something
  }
  if ($that) {
    // Do something else
  }
}

因此,如果我尝试仅为$that传递一个值,我可以执行以下操作:

$that = 100;
this('',$that);

但是''是将 NULL 传递给函数的最佳方式吗?

使用 null

$that = 100;
test(null,$that);

> 将null值传递给 PHP 函数的最佳方法如下:

someFunction(null);

然后,要检查null是否在函数内部传递,请使用 $param === nullis_null($param)

您需要找到空字符串和 null 之间的区别。这是不一样的。

最好的方法是使用

 test(null, $that);

如果您使用===运算符,您将看到差异