6

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?

1
  • 1
    Call like validate($lname, 0,'name') Commented Nov 9, 2011 at 12:27

5 Answers 5

22

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); Uncaught ArgumentCountError starting with PHP 7.1rfc (and starting with PHP 8.0 as well for internal functionsrfc).

  • 2 before PHP 8.0, see Named Arguments

Sign up to request clarification or add additional context in comments.

Comments

6

You should at least set the $type in this line:

function validate($data, $data2 = 0, $type)

at NULL or '' as you can see here:

function validate($data, $data2 = 0, $type = null)

PHP let you to set a value for the parameters, but you can't define a parameter WITHOUT a preset value AFTER parameter(s) which HAVE a preset value. So if you need to always specify the third param, you have to switch the second and the third like this:

function validate($data, $type, $data2 = 0)

Comments

0

From 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)
{ ....

Comments

0
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)

Comments

0

Notice that starting with PHP 7.1 this will throw a PHP Fatal error, not just a warning:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.