Returns:PHP Code:$function='create_function("$a","return $a;");';
$newfunct=eval($function);
echo $newfunct("DISPLAY ME");
Fatal error: Function name must be a string in c:\program files\wamp\www\phpd\phpd.php on line 32
| SitePoint Sponsor |
Returns:PHP Code:$function='create_function("$a","return $a;");';
$newfunct=eval($function);
echo $newfunct("DISPLAY ME");
Fatal error: Function name must be a string in c:\program files\wamp\www\phpd\phpd.php on line 32
Try that. And it'll be $function that's the function, not $newfunct. (Why are you using eval?)PHP Code:$function='create_function(\'$a\',\'return $a;\');';
$newfunct=eval($function);
echo $newfunct('DISPLAY ME');
- Nathan
Returns the same error.Originally Posted by someonewhois
Why are you even using eval?
- Nathan
Also, what line is 32? If it's the function-call one, then that's because you need to actually catch the output to create_function.
- Nathan
I'm using eval because I want to generate the function on the runtime, and because it gives an unique name of the each newly created function.
Can you post the entire code? It'll make it a lot easier.
- Nathan
I managed it working like this:
But within eval:PHP Code:$newfunc=create_function('$a,$b', 'return $a;');
Returns:PHP Code:eval("$newfunc=create_function('$a,$b', 'return $a;');");
Parse error: syntax error, unexpected '=', expecting '}' in c:\program files\wamp\www\phpd\phpd.php on line 32
And so I ask once again -- why are you using eval? It's completely unnecessary.
- Nathan
Because I want to use custom function within another php code,the function may be called twice, and I don't want it to cause redeclare error. Then there will be just two functions with different names and identical bodies.
Sorry, what? You can't get redeclaration errors on create_function. On top of that, eval won't change that at all. Why don't you just declare the function ABOVE the block of code, so you don't have anything duplicated.
- Nathan
Thank you very much for your quick response.
But if I needed it declared before the body, I would do it without the eval(). I'm just wondering why the eval(), returns such a message.
I forgot to mention that if it is declared with function fn_name(){} in eval, it works fine.
Looks like = makes the problem.
The reason it doesn't work in eval is because double quotes parses the variable out of it, which results in "=create...". There's no variable. Add a backslash in front of the dollar sign (or use single quotes) to fix it.
- Nathan
Bookmarks