早期的 PHP 版本允许函数调用时,传递的参数少于函数定义本身要求的参数个数。当你调用函数时就会抛出一个参数丢失的警告。

// PHP 5.6

function sum($a, $b)
{
    return $a + $b;
}

sum(); 
// Warning: Missing argument 1 for sum()
// Warning: Missing argument 2 for sum()

sum(3);
// Warning: Missing argument 2 for sum()

在这种情况下警告没什么用,开发者必须自行检查参数是否正确。在 PHP 7.1 中,这些警告变成了一个 ArgumentCountError 的异常:

// PHP 7.1

function sum($a, $b)
{
    return $a + $b;
}

sum(); 
// Fatal error: Uncaught ArgumentCountError: Too few arguments to function sum(), 0 passed in /vagrant/index.php on line 18 and exactly 2 expected in /vagrant/index.php:13

sum(3); // skipped

sum(3, 4); // skipped 

 

相关文章:

  • 2022-01-22
  • 2021-05-30
  • 2022-01-24
  • 2021-06-25
  • 2022-12-23
  • 2021-11-26
  • 2022-12-23
  • 2021-12-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2021-11-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案