Skip to content
Advertisement

Why does PHP not throw an error when I pass too many parameters to a function?

I am a n00b at php. I was learning about Default Parameters so I made this function.

JavaScript

I made these calls

JavaScript

The first two printed what was expected i.e

JavaScript

but the third call also printed

JavaScript

I was expecting an error, after all the function is made for one argument whereas I am calling it with two arguments.
Why was there no error?

Advertisement

Answer

It is not wrong to pass more arguments to a function than needed.

You only get error if you pass to few arguments.

JavaScript

Above will result in following error:
Uncaught ArgumentCountError: Too few arguments to function…

If you want to fetch first argument plus all others arguments passed to function you can do:

JavaScript

Resulting in:
string(5) “test1” array(2) { [0]=> string(5) “test2” [1]=> string(5) “test3” }

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement