在没有默认值的参数前面带有默认值的函数参数:会发生什么?

(Perhaps this question has already been asked before, but I can't find it.)

What happens if you have an argument with default value preceding an argument without default value in PHP? Just like this:

function myfunction($foo = 12, $bar) {
    ...
}
  • Does PHP give errors and if yes, on which error reporting level?
  • What happens when you call myfunction("hello") with only one argument?

Yes, it will output a warning, not an error, namely:

Warning: Missing argument 2 in call to myfunction() in FILE on line LINENO

If you call myfunction("hello"), $bar is undefined, so it will either be an empty string or NULL, and $foo = "hello". It will only raise a warning though, so your script will still execute.

Why don't you just switch the order of the parameters?

Edit: Here is a good explanation of why it is not possible to overload standalone functions in PHP: PHP function overloading