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!

Alan
  • 389
  • 1
  • 7
  • 16

5 Answers5

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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! – Alan Sep 07 '12 at 03:10
3

All you need to do is:

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

Reference: variable functions.

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • 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! – Alan Sep 07 '12 at 02:32
  • `$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. – Matthew Sep 07 '12 at 02:38
  • I have a few bugs to work out, but this worked. I guess I was just overcomplicating it! Thanks! – Alan Sep 07 '12 at 02:54
  • (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.) –  Sep 07 '12 at 03:08
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);');
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Thanks xdazz. Per Matthew's solution above I figured out that I was just overcomplicating this! – Alan Sep 07 '12 at 03:11
  • @pst Yes, `eval` should not be used, i just mean to point out why `eval` in the op's code not work. – xdazz Sep 07 '12 at 03:13
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.

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
  • The solution provided by @Matthew above worked - but I really appreciate your help as well!! – Alan Sep 07 '12 at 02:55
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?

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 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 Sep 07 '12 at 02:27
  • 1
    @Alan have a look at [this](http://changelog.ca/log/2005/06/14/the-proper-way-to-use-php-eval) – John Woo Sep 07 '12 at 02:30
  • 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! – Alan Sep 07 '12 at 03:09