-1

index.php:

require 'modulename.php';
$keyword = $_GET['q'];
getResults();

modulename.php:

$config = ... ;
$service = ... ;
function getResults($config, $service, $keyword) {
   ...
}

... but this throws an error:

Missing argument 2 for getResults(), called in index.php on line xx and defined in modulename.php

... it seems the function is not using the already defined variables, how do I make it use those?

6
  • 1
    change function getResults($config, $service, $keyword) to function getResults($config = false, $service = false, $keyword = false) to make them optional if thats what you want Commented Jul 25, 2015 at 7:48
  • you must pass parameters to function call or use default values for all parameters in function defination. Commented Jul 25, 2015 at 7:48
  • @anonymous But that would just make them optional, instead I want to pass those parameters. They exist in the same module with the same names. Commented Jul 25, 2015 at 7:50
  • @Sagar $config and $service is already defined outside the function, I just need to use them as function arguments. Commented Jul 25, 2015 at 7:56
  • @3zzy: then do not pass those 2 variables into function definition, directly access those variables within function scope or function body. Commented Jul 25, 2015 at 7:58

3 Answers 3

1

You created the function getResults($config, $service, $keyword) which require three parameters to call but you called it without any parameter. You can try this

in index.php

require 'modulename.php';
$keyword = $_GET['q'];
getResults($keyword );

modulename.php

function getResults($keyword = '') {
    $config = ... ;
    $service = ... ;
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

But the config is already defined, I don't want to redefine it inside the function, just want to use that variables of the same names that are defined outside of the function.
So can define $config as global
0

Your getResults(); need parameter

getResults($config, $service, $keyword);

If you don't have your parameter then set by default false

getResults($config=false, $service=false, $keyword)

3 Comments

$config and $service is already defined outside the function, I just need to use them as function arguments.
@3zzy check this link
You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.
0

The error message states that arg 2 is missing - in your function that would refer to $keyword. In the example code you provided you define both $config & $service but not $keyword - so, by provising default values for the function parameters you would eliminate the error. However, if each and every parameter is required then by testing for their value not being false ( which is their default value in this version of the function ) then the function will do nothing.

Alternatively, define each variable before calling the function:-

$config='';
$service='';
$keyword='';


function getResults($config=false, $service=false, $keyword=false) {
   if( $config && $service && $keyword ){
       ...
   }
}

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.