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
Missing argument 3 ($type) for validate()
How can I fix that?
Advertisement
Answer
Missing argument 3 ($type) for validate() [1]
Always list optional arguments as the last arguments, never before non-optional arguments.
Since PHP doesn’t have named parameters1 nor “overloading ala Java”, that’s the only way:
function validate($data, $type, $data2 = 0) { }
1 Error with severity
E_WARNING
until PHP 7.0 (including); UncaughtArgumentCountError
starting with PHP 7.1rfc (and starting with PHP 8.0 as well for internal functionsrfc).2 before PHP 8.0, see Named Arguments