0

I have few php functions that returns various data via same variable, and that variable should assign to a array. Now I want to execute function by checking $_POST and assign $data to $response multidimensional array... Is there any way to do that ???

$functionname = $_POST['functionname'];

function testOne(){
$data = array('test'=>'value1');
return $data;
}

function testTwo(){
$data = array('test'=>'value2');
return $data;
}

//Here I need to execte each functions and return $data

$response = array('result' => array('response'=>'success'),'clients' => $data);

print_r($response);
1
  • 1
    Do you mean $data = $functioname()? If so, that is a very insecure way of writing code. Commented Jul 25, 2012 at 18:28

3 Answers 3

1

Functions are only run when they're called. You haven't called either of your functions.

From the looks of your code, I assume that $functionname will take a value of either testOne or testTwo, which then tells the code what function to run. What you want to do, then, is call the function using the variable function name and capture the returned value into a variable:

$functionname = $_POST['functionname'];
//function definitions
$response = array('result' => array('response'=>'success'), 'clients' => $functionname());

See the docs for, well... the docs.

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

3 Comments

You need to validate and sanitize the input. Now anybody can call any PHP or user function. Granted, they cannot supply parameters, but it's still an extremely bad way of doing this.
@stackminu what happens when someone hacks your code ... that function could be anything ....
@stackminu What they said. I just gave you a basic "here's how to do what you want"; you need to secure it to make sure it's used the way you want it to be used.
1

you can call the function inside of an array directly.

$response = array('result' => array('response'=>'success'),'clients' => testTwo());

now in $response['clients'] the value will contain array('test'=>'value2');

or if you want to call a function through user input, for example. if

if $_POST['funtionname'] = 'testOne'; then execute testOne();
if $_POST['funtionname'] = 'testTwo'; then execute testTwo();

then you can make use of call_user_func() here. like this.

$_POST['funtionname'] = 'testOne';
call_user_func($_POST['functionname']);
//this will execute testOne(); and depending upon the value it consist, it will execute the corresponding function.

if that is what you mean. correct me if i understood it wrong.

2 Comments

But I want to call that function using $functionname = $_POST['functionname']; Because user(client) requests what function he wants to execute. [This is a API] :)
thanks a lot for the answer. @Palladium told me a easy way to do this. :)
0

I think that you want to call the function from within the $functionname variable .. if so this is how you do it :

$data = call_user_func($functionname);
$response = array('result' => array('response'=>'success'),'clients' => $data);

in this case the value of $functionname should either be testOne or testTwo

Docs for call_user_func here

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.