1

I need help figuring out the syntax for the eval function - or even if eval is the right approach. I have a column in my mysql database which holds the name of a PHP function I need to run. There are also PHP variables that would like to leave as variable until they are passed to the function. Below is what I have so far:

eval($valRec[$key]($key,$value));

$valRec is the array which contains the results of a mysql SELECT. $key is a variable which references the name of the column that contains the function name.

$key and $value are the PHP variables that I need to pass into the function.

In the end, I want to end up with:

functionName($key,$value);

which PHP should run.

Hopefully I explained it clearly - thank you in advance for any help!

2
  • can you var_dump($valRec); ?? Commented Sep 7, 2012 at 2:24
  • never never use eval. There are a fewwwww corner cases where it may be necessary, but I can pretty much guarentee you will never run into them. Commented Sep 7, 2012 at 3:29

5 Answers 5

4

Eval is probably not what you want. Look at call_user_func and call_user_func_array. They let you call a function whose name is in a variable.

call_user_func($valRec[$key], $key, $value);
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't end up using eval as Matthew's solution worked - but I will definitely keep call_user_func in mind in case I ever run into a situation where I need something like this again!
3

All you need to do is:

$valRec[$key]($key,$value);

Reference: variable functions.

4 Comments

Could it really be that simple?!? I have a couple tests that I'm going to be running in the next few minutes - will let you know if that works!
$foo = 'bar'; $foo(); Calls function bar. That's all that is happening here, except that the string is contained in an array. Rarely is eval() ever needed in PHP.
I have a few bugs to work out, but this worked. I guess I was just overcomplicating it! Thanks!
(Just be aware that "variable functions" are like "variable variables" and do not represent first-class functions; the variable must still resolve to the name of a function.)
2

In php, you could do below;

$func = 'strtolower';
$foo = $func($bar);

So in your case, $valRec[$key]($key,$value); will just work.

Check the demo.

Addtion: The reason why your eval not work is because eval need to take a string as parameter, not don't forget ; to end the statement, or it will be syntax error. So you need to do:

eval($valRec[$key].'($key, $value);');

2 Comments

Thanks xdazz. Per Matthew's solution above I figured out that I was just overcomplicating this!
@pst Yes, eval should not be used, i just mean to point out why eval in the op's code not work.
1

I haven't used PHP recently, but the concept of eval is to evaluate a string.

As such, you need to create the string representing the line of code you want run.

In your case that means:

eval($valRec[$key] . '($key, $value);');

You've said it yourself, you want to end up with functionName($key,$value); so you need to make a string that is that, then pass it to eval.

1 Comment

The solution provided by @Matthew above worked - but I really appreciate your help as well!!
0

see this example,

<?php
    $string = 'cup';
    $name = 'coffee';
    $str = 'This is a $string with my $name in it.';
    echo $str. "\n";
    eval("\$str = \"$str\";");
    echo $str. "\n";
?>

this is the output

This is a $string with my $name in it.
This is a cup with my coffee in it.

you need to read this, when is eval evil in php?

3 Comments

John - thanks for the example. This is very similar to the example in the PHP manual - which I read over and tried to apply unsuccessfully.
@Alan have a look at this
Thanks for the link! Per the solution provided by Matthew I didn't end up using eval - but I will make sure to remember this if I end up using it in the future!

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.