PHP 函数缺少参数错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8065003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP function missing argument error
提问by Tural Ali
My validate function looks like that
我的验证功能看起来像这样
function validate($data, $data2 = 0, $type)
{
...
Function call example
函数调用示例
if ($result = validate($lname, 'name') !== true)
response(0, $result, 'lname');
As you see, my validate function has 3 input vars. I'm not using second var - $data2 often, that's why set it to 0 by default. But when I'm calling this function as given example (as far as I know it means $data=$lname, $data2=0, $type='name') getting error message
如您所见,我的验证函数有 3 个输入变量。我不经常使用第二个 var - $data2,这就是默认情况下将其设置为 0 的原因。但是当我按照给定的例子调用这个函数时(据我所知,这意味着 $data=$lname, $data2=0, $type='name') 收到错误消息
Missing argument 3 ($type) for validate()
How can I fix that?
我该如何解决?
回答by Berry Langerak
Missing argument 3 ($type) for validate()
validate() 缺少参数 3 ($type)
Alwayslist optional arguments as the last arguments. Since PHP doesn't have named parameters nor "overloading ala Java", that's the only way:
始终将可选参数列为最后一个参数。由于 PHP 没有命名参数,也没有“重载 ala Java”,这是唯一的方法:
function validate($data, $type, $data2 = 0) {
}
回答by Aurelio De Rosa
You should at least set the $type in this line:
您至少应该在这一行中设置 $type:
function validate($data, $data2 = 0, $type)
at NULL
or ''
as you can see here:
在NULL
或''
如您所见:
function validate($data, $data2 = 0, $type = null)
PHP let you to set a value for the parameters, but you can't define a parameter WITHOUTa preset value AFTERparameter(s) which HAVEa preset value. So if you need to always specify the third param, you have to switch the second and the third like this:
PHP让您能够为参数设置一个值,但不能定义参数无预设值后的参数(S),其HAVE预设值。因此,如果您需要始终指定第三个参数,则必须像这样切换第二个和第三个参数:
function validate($data, $type, $data2 = 0)
回答by Aziz
From http://php.net/manual/en/functions.arguments.php
来自http://php.net/manual/en/functions.arguments.php
Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected
请注意,使用默认参数时,任何默认值都应位于任何非默认参数的右侧;否则,事情将不会按预期进行
Your should switch the second and third arguments of the function, making the optional argument the last one. So it becomes:
您应该切换函数的第二个和第三个参数,使可选参数成为最后一个。所以就变成了:
function validate($data, $type, $data2 = 0)
{ ....
回答by ahmed
function validate($data, $data2, $data3, $data4, $data5)
im a beginner but i think that you can use a thousand arguments as long as you call like that
我是初学者,但我认为只要你这样调用,你就可以使用一千个参数
if ($result = validate($lname, 'name','','','') !== true)
回答by user832146
Notice that starting with PHP 7.1 this will throw a PHP Fatal error, not just a warning:
请注意,从 PHP 7.1 开始,这将引发 PHP 致命错误,而不仅仅是警告:
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function validate(), 2 passed in /path/to/file.php on line X and exactly 3 expected
More info: http://php.net/manual/en/migration71.incompatible.php